一、luaL_dofile(): cannot open ./lib.lua: No such file or directory

问题原因

运行应用的目录和lua文件不在同一个目录,.lua文件所在的目录是当前所在的目录而不是程序所在目录。

ma@ubuntu:/data/code/lua/2-lua和c++/cpp-lua$ tree
.
├── debug
│   └── app
├── lib.lua
├── main.cpp
└── Makefile

1 directory, 6 files

app在当前目录下的debug目录下,在当前目录下执行./debug/app可以成功读取到lib.lua,但是在debug目录下执行./app就会报错。

二、lua_pcall(): attempt to call a nil value

代码中向栈压入了空元素,执行了以下操作:

bRet = lua_pcall(L,0,0,0);

这是在网上看到的代码,别人运行通过。

猜测可能是版本的原因,在lua5.4报错,删掉这块代码即可。

三、undefined reference to ''*''

使用c++调用lua时报错:

ma@ubuntu$ make
g++ main.cpp --std=c++11 -o debug/app
/tmp/cc2CFAYl.o: In function `main'':
main.cpp:(.text+0x9): undefined reference to `luaL_newstate''
main.cpp:(.text+0x50): undefined reference to `luaL_loadfilex''
main.cpp:(.text+0xa6): undefined reference to `lua_pcallk''
main.cpp:(.text+0xe6): undefined reference to `lua_getglobal''
main.cpp:(.text+0x134): undefined reference to `lua_pushnumber''
main.cpp:(.text+0x153): undefined reference to `lua_pushnumber''
main.cpp:(.text+0x17a): undefined reference to `lua_pcallk''
main.cpp:(.text+0x199): undefined reference to `lua_tolstring''
main.cpp:(.text+0x1c7): undefined reference to `lua_close''
main.cpp:(.text+0x1e2): undefined reference to `lua_isnumber''
main.cpp:(.text+0x201): undefined reference to `lua_tonumberx''
main.cpp:(.text+0x25e): undefined reference to `lua_close''
collect2: error: ld returned 1 exit status
Makefile:10: recipe for target ''app'' failed
make: *** [app] Error 1

原因是没有找到对应的函数定义,编译时加上lua的链接库即可解决:

g++ main.cpp --std=c++11 /usr/local/lib/liblua.a -o debug/app
最后修改:2018 年 04 月 28 日
如果觉得我的文章对你有用,请随意赞赏