lua加载我的c ++共享库,但不加载它的依赖共享库

时间:2017-10-26 08:30:09

标签: c++ lua shared-libraries lua-c++-connection

我有一个c ++(遗留)应用程序,它调用一些lua脚本来实现某些功能。

现在我正在编写一个新的c ++库,应该从该lua脚本中调用。

#include <lua.hpp>

extern "C" {

static int isquare(lua_State *L){              /* Internal name of func */
        return 1;                              /* One return value */
}
static int icube(lua_State *L){                /* Internal name of func */
        return 1;                              /* One return value */
}


/* Register this file's functions with the
 * luaopen_libraryname() function, where libraryname
 * is the name of the compiled .so output. In other words
 * it's the filename (but not extension) after the -o
 * in the cc command.
 *
 * So for instance, if your cc command has -o power.so then
 * this function would be called luaopen_power().
 *
 * This function should contain lua_register() commands for
 * each function you want available from Lua.
 *
*/
int luaopen_power(lua_State *L){
        printf("before power open");
        lua_register(
                        L,               /* Lua state variable */
                        "square",        /* func name as known in Lua */
                        isquare          /* func name in this file */
                        );

        lua_register(L,"cube",icube);
        printf("after power register");
        return 0;
}
}

g++ -Wall -shared -fPIC -o power.so -I/usr/include/lua5.1  hellofunc.cpp -lstdc++

我没有提及任何lua5.1 so文件进行链接。

但是这个power.so在运行时需要lua-5.1.so。

现在,我有一个C ++遗留应用程序,其中编译了lua52。

它调用alert.lua进行一些工作。

package.cpath = package.cpath .. ";/usr/lib64/power.so"
package.cpath = package.cpath .. ";/usr/lib64/liblua-5.1.so"
require("power")

注意:加载power.so的lua在lua5.2上运行

Power.so已编译并依赖于lua5.1

我收到错误

 undefined symbol: lua_setfield'

这些版本必须相同吗?

有人可以解释这个问题吗?

编辑:如果我使用lua52.so编译power.so,那么lua脚本和C ++应用程序会异常中止。

如果在构建power.so时没有提及-llua52那么在运行时会出现一个错误,说明未定义的符号。

编辑:更多说明:

有一个C ++应用程序.exe。 (samplecpp) 还有一个.dll / .sh与lua 5.2库一起构建,因此具有lua和其他功能。 (luaplugin.so)

这个luaplugin.so可以调用任何配置的lua脚本。它在lua脚本中调用和执行函数。

现在我有一个lua脚本,我想连接到另一个c ++模块。

c ++模块(构建到.so依赖于lua52.so)我正在编写使用lua函数进行注册等。因为它必须从lua脚本加载。

但是在运行时,当samplecpp执行lua脚本并且当luascript需要c ++ .so时,我在c ++ .so中使用的lua函数得到了未解决的错误。

如何让它引用samplecpp本身可用的lua函数?

1 个答案:

答案 0 :(得分:0)

是的,您需要使用为相同版本的Lua编译的C / C ++库。

不同版本的Lua之间没有ABI兼容性 看到 http://www.lua.org/versions.html#numbering