jcmoyer / rust-lua53

Lua 5.3 bindings for Rust
MIT License
158 stars 45 forks source link

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

Open deepakjois opened 7 years ago

deepakjois commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

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