嵌入minilua.h到C++项目后加载原生Lua库提示缺失lua_type符号的问题求助
嵌入minilua.h到C++项目后加载原生Lua库提示缺失lua_type符号的问题求助
各位大佬好,我现在正尝试把Lua通过minilua.h嵌入到我的C++项目里。纯Lua脚本跑起来没问题,但只要用到原生库就会报错说找不到lua_type符号,可我用nm命令查过,这个符号明明在生成的二进制文件里啊!有人建议用extern "C",但试了之后也没解决。感觉问题应该很简单,但就是找不到方向,不知道该搜什么关键词,也不知道怎么修复。我的最终目标是做一个自定义Lua运行时,既能跑Lua脚本也能调用原生库,最终生成单一二进制文件。
我的代码(main.cpp)
#include <string> #define LUA_IMPL #include "lua/minilua.h" using std::string; class Lua { public: lua_State *L; Lua() { L = luaL_newstate(); luaL_openlibs(L); } ~Lua() { lua_close(L); } void run(string s) { luaL_loadstring(L, s.c_str()); lua_call(L, 0, 0); } }; int main(int argc, char *argv[]) { // Works Lua().run("print('hello world')"); // Barfs error Lua().run(R"( package.path = "./lualib/?.lua;./lualib/?/init.lua;" .. package.path package.cpath = "./lualib/?.so;./lualib/?/init.so;" .. package.cpath local pegasus = require 'pegasus' local server = pegasus:new({ port = '8080', location = '.', }) server:start(function(req, res) print('started') res:addHeader('Cache-Control', 'no-cache') :write("hello world") end) )"); }
构建命令与运行错误
~/code/cpp/luastuff g++ -I./lib main.cpp -o game && ./game hello world PANIC: unprotected error in call to Lua API (error loading module 'socket.core' from file './lualib/socket/core.so': dlopen failed: cannot locate symbol "lua_type" referenced by "/data/data/com.termux/files/home/code/cpp/luastuff/lualib/socket/core.so"...) Aborted
符号检查结果
~/code/cpp/luastuff nm game | grep lua_type 00000000000360e0 T lua_type 0000000000036164 T lua_typename
有没有大佬能指点一下问题出在哪,该怎么解决?
内容来源于stack exchange




