一、luaL_dofile(): cannot open ./lib.lua: No such file or directory
问题原因
运行应用的目录和lua文件不在同一个目录,.lua
文件所在的目录是当前所在的目录而不是程序所在目录。
1 2 3 4 5 6 7 8 9 |
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
代码中向栈压入了空元素,执行了以下操作:
1 |
bRet = lua_pcall(L,0,0,0); |
这是在网上看到的代码,别人运行通过。
猜测可能是版本的原因,在
lua5.4
报错,删掉这块代码即可。
三、undefined reference to '***'
使用c++调用lua时报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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
的链接库即可解决:
1 |
g++ main.cpp --std=c++11 /usr/local/lib/liblua.a -o debug/app |
评论