如何用Lua构建C程序
测试c:如何用Lua构建C程序,c,lua,C,Lua,测试c: #include "lua.h" #include "lauxlib.h" #include "lualib.h" int main(){ lua_State *L = luaL_newState(); /* opens Lua */ luaL_openlibs(L); /* opens the standard libraries */ luaL_dofile(L,"test.lua"); /* runs Lua scrip */ lua_close(L
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
int main(){
lua_State *L = luaL_newState(); /* opens Lua */
luaL_openlibs(L); /* opens the standard libraries */
luaL_dofile(L,"test.lua"); /* runs Lua scrip */
lua_close(L);
return 0;
}
建造:
gcc -o test test.c -I/usr/local/Cellar/lua/5.1.5/include
然后我得到了一个错误:
test.c:7:18: warning: implicit declaration of function 'luaL_newState' is
invalid in C99 [-Wimplicit-function-declaration]
lua_State *L = luaL_newState(); /* opens Lua */
^
test.c:7:14: warning: incompatible integer to pointer conversion initializing
'lua_State *' (aka 'struct lua_State *') with an expression of type 'int'
[-Wint-conversion]
lua_State *L = luaL_newState(); /* opens Lua */
^ ~~~~~~~~~~~~~~~
2 warnings generated.
Undefined symbols for architecture x86_64:
"_luaL_loadfile", referenced from:
_main in test-dxPwkn.o
"_luaL_newState", referenced from:
_main in test-dxPwkn.o
"_luaL_openlibs", referenced from:
_main in test-dxPwkn.o
"_lua_close", referenced from:
_main in test-dxPwkn.o
"_lua_pcall", referenced from:
_main in test-dxPwkn.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我通过自制软件安装了Lua:
ls /usr/local/Cellar/lua/5.1.5/include
lauxlib.h lua.h lua.hpp luaconf.h lualib.h
有人知道如何解决这个错误吗?谢谢。制作时,您应该在/usr/local/lib中找到lua的.so文件,找到它,并使用-L添加库路径,使用-L链接库
最后,在make步骤之后,如果程序无法运行,您还应该使用ln-s添加与/usr/lib中的.so相关的符号链接。它是
luaL\u newstate
,而不是luaL\u newstate
在命令行的末尾还需要
-llua-lm
在构建示例时,您需要添加导入库或lua*.so
共享对象。未定义的引用来自链接器,不知道那些lua C函数在哪里。我不确定这将在您的linux设置中的什么位置。也许可以尝试使用哪个lua51.so
或查看您的bin目录。同时将-I
参数移动到test.c
之前,这将修复警告。