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