Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error loading Lua C Modules on Linux and OS X #85

Open
deepakjois opened this issue Jun 21, 2017 · 5 comments
Open

Error loading Lua C Modules on Linux and OS X #85

deepakjois opened this issue Jun 21, 2017 · 5 comments

Comments

@deepakjois
Copy link
Contributor

This is a separate error from #84. This occurs on both OS X and Linux. In the code snippet below: serpent is a regular Lua module and lfs (luafilesystem) is a C module.

extern crate lua;

fn main() {
  let mut state = lua::State::new();
    state.open_libs();
    state.do_string("
    print('hello')

    if not pcall(function() require('serpent') end) then
      print('error loading serpent')
    else
        spt = require('serpent')
        print('serpent successfully loaded')
    end

    local status, err = pcall(function() require('lfs') end)
    print(err)


    ");
}

On Linux it gives the following error:

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/rlua`
hello
serpent successfully loaded
error loading module 'lfs' from file '/home/deepak/lua53/lib/lua/5.3/lfs.so':
        /home/deepak/lua53/lib/lua/5.3/lfs.so: undefined symbol: lua_gettop

On OS X (after fixing #84), it gives the following error:

  Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/rlua`
hello
serpent successfully loaded
error loading module 'lfs' from file '/Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so':
        dlopen(/Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so, 6): Symbol not found: _lua_settable
  Referenced from: /Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so
  Expected in: flat namespace
 in /Users/deepak/Downloads/lua53/lib/lua/5.3/lfs.so
@deepakjois
Copy link
Contributor Author

@Parakleta, it seems you posted something similar here:
https://users.rust-lang.org/t/exporting-dynamic-symbols-from-executable/6103/3

Do you have any idea what’s going on here?

@Parakleta
Copy link

What I posted there is probably exactly the issue you're having. Essentially the lfs.so module needs the be able to see the Lua API functions in the executable, and a chain of compile options are required to keep them visible to the end. I haven't compiled this stuff in ages so I don't know exactly what is still relevant.

You'll need the use the nm utility on the output of the various compile stages to work out where the symbols are being lost. Have you tried creating the dynsyms.txt file and the custom build rule from the linked post? Also try setting the LUA_API and LUALIB_API to set default visibility in luaconf.h I think.

@deepakjois
Copy link
Contributor Author

On Linux, when I put this is my .cargo/config file, things work fine and the Lua C module loads successfully:

[build]
rustflags = ["-C", "link-args=-Wl,-export-dynamic"]

@deepakjois
Copy link
Contributor Author

deepakjois commented Jun 22, 2017

On OS X, I have to tweak the linker flags slightly:

[build]
rustflags = ["-C", "link-args=-Wl,-export_dynamic"]

See the underscore instead of the dash, i.e. export_dynamic instead of export-dynamic.

@deepakjois
Copy link
Contributor Author

After doing a little bit more research it appears that a more cross-platform solution is putting this in .cargo/config

[build]
rustflags = ["-C", "link-args=-rdynamic"]

Discovered this via a tip here: https://stackoverflow.com/questions/34082636/expose-symbols-to-dynamic-linker-when-linking-with-native-library-in-rust

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants