【问题标题】:lua loads my c++ shared library but not its dependent shared librarieslua 加载我的 c++ 共享库,但不加载它的依赖共享库
【发布时间】:2018-04-07 13:01:28
【问题描述】:

我有一个 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 函数?

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-06-19
      • 2011-01-01
      • 2011-01-20
      • 1970-01-01
      • 2019-02-10
      • 2011-07-22
      • 1970-01-01
      • 2019-04-17
      • 2011-11-07
      相关资源
      最近更新 更多