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

标签 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,那么在运行时会出现错误,指出 undefined symbol 。

编辑:更多解释:

有一个 C++ 应用程序 .exe。 (示例cpp) 还有一个 .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 函数上遇到 Unresolved 错误。

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

最佳答案

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

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

关于c++ - lua 加载我的 C++ 共享库但不加载它的依赖共享库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46949349/

相关文章:

lua - Lua 中的独家模式匹配

java - 在接口(interface)中提供不同的方法名称

c++ - 创建进程 : redirect child stdout to parent stdout in C++/Windows

c++ - 在 Cmake 中静态链接 Lua

c++ - 为什么我可以在 C++ 中的函数中定义结构和类?

c++ - 可以将 Lua 转换为 spir-v 吗?

deployment - 我如何在 glassfish 中使用共享库来避免部署巨大的库?

c - 是否可以使用共享对象构造函数来设置库搜索路径?

C++ 为多米诺骨牌生成随机数

c++ - 为 STL 映射 : "cannot bind lvalue" 重载 << 和 >>