如何用Lua构建C程序?

时间:2021-07-14 02:49:32

test.c :

测试。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);
  return 0;

}

build:

构建:

gcc -o test test.c -I/usr/local/Cellar/lua/5.1.5/include

then i got error:

然后我有错误:

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)

I installed Lua via Homebrew:

我通过自制程序安装了Lua:

ls /usr/local/Cellar/lua/5.1.5/include
lauxlib.h lua.h     lua.hpp   luaconf.h lualib.h

Anyone knows how to solve this error? Thanks.

有人知道如何解决这个错误吗?谢谢。

2 个解决方案

#1


5  

It's luaL_newstate, not luaL_newState.

这是luaL_newstate,不是luaL_newstate。

You also need -llua -lm at the end of the command line.

您还需要在命令行结束时使用-llua -lm。

#2


0  

when u make it, u should ld the .so file of lua which may be in /usr/local/lib, find out it, and use the -L to add the library path, use -l to link the library.

当你做的时候,你应该把lua的文件放在/usr/local/lib中,找到它,然后使用-L添加库路径,使用-L链接库。

finally, after the make step, if the program cannot run, u should also use ln -s to add a symbol link which is related to the .so in /usr/lib.

最后,在make步骤之后,如果程序不能运行,你也应该使用ln -s来添加一个符号链接,它与so in /usr/lib相关。

#1


5  

It's luaL_newstate, not luaL_newState.

这是luaL_newstate,不是luaL_newstate。

You also need -llua -lm at the end of the command line.

您还需要在命令行结束时使用-llua -lm。

#2


0  

when u make it, u should ld the .so file of lua which may be in /usr/local/lib, find out it, and use the -L to add the library path, use -l to link the library.

当你做的时候,你应该把lua的文件放在/usr/local/lib中,找到它,然后使用-L添加库路径,使用-L链接库。

finally, after the make step, if the program cannot run, u should also use ln -s to add a symbol link which is related to the .so in /usr/lib.

最后,在make步骤之后,如果程序不能运行,你也应该使用ln -s来添加一个符号链接,它与so in /usr/lib相关。