cpjreynolds / lua53-sys

Rust bindings to Lua 5.3
MIT License
0 stars 0 forks source link

Loading C modules does not work #1

Open deepakjois opened 7 years ago

deepakjois commented 7 years ago

I installed LuaRocks modules serpent (regular Lua module) and luafilesystem (Lua C Module) inside my Lua environment, and then tried to run this modified hello.rs file.

extern crate lua53_sys;
extern crate libc;

use lua53_sys::{lauxlib, lua};

pub const HELLO: &'static [u8] = b"
print('hello')
local spt = require('serpent')
print('loaded serpent successfully')

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

\0";

fn main() {
    unsafe {
        let state = lauxlib::luaL_newstate();
        lauxlib::luaL_openlibs(state);
        lauxlib::luaL_dostring(state, HELLO.as_ptr() as *const libc::c_char);
        lua::lua_close(state);
    }
}

The error I get is:

$ cargo run --example hello
   Compiling lua53-sys v0.1.1 (file:///home/deepak/rust-playg/lua53-sys)
    Finished dev [unoptimized + debuginfo] target(s) in 0.93 secs
     Running `target/debug/examples/hello`
hello
loaded serpent successfully
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

I am also tracking this at jcmoyer/rust-lua53#85. It is most likely an issue with the Rust linker not exporting the Lua symbols properly so that a Lua .so module can see it.

I will continue to investigate this, but if you know of any ways to solve this, please do comment.

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

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