开发者

Unable to dynamically load a library in Lua with package.loadlib

I'm trying to create a library that I can load into Lua with require or package.loadlib, but I have so far been unsuccessful. The library itself is in C++, but as far as I can tell I've taken the step necessary to properly export the function to load the library. In a nutshell, this is the relevant section of my C/C++ code:

extern "C"
{

// This line is copied from http://gcc.gnu.org/wiki/Visibility
// it's actually in a header, including it here for brevity
#define EXPORT __attribute__((visibility("default")))

EXPORT int luaopen_foo(lua_State* L)
{
    开发者_如何学GoluaL_register(L, "Foo", fooL_table);
    return 0;
}

}

In my Lua script, I have this:

mylib = package.loadlib("libfoo.so", "luaopen_foo")
print(mylib) -- prints "nil"

The library is being created from a Makefile generated by CMake, and in the CMakeLists.txt I have tried compiling with various options, such as

add_library(foo STATIC ${foo_SOURCES})
add_library(foo MODULE ${foo_SOURCES})
add_library(foo SHARED ${foo_SOURCES})

And none of these options appear to work.

Are there any steps I'm missing to make this work? I'm having a hard time finding information on how to do this properly online so any guidance is welcome. I'm using is Ubuntu with gcc to compile.


In the Lua manual, it says, "libname must be the complete file name of the C library, including if necessary a path and extension." Is your .so file in that directory, with that name?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜