如何用Lua构建C程序

时间:2013-12-08 04:06:03

标签: c lua

test.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;

}

构建

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)

我通过Homebrew安装了Lua:

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

任何人都知道如何解决此错误?感谢。

2 个答案:

答案 0 :(得分:5)

它是luaL_newstate,而不是luaL_newState

您还需要在命令行末尾添加-llua -lm

答案 1 :(得分:0)

当你创建它时,你应该找到可能位于/ usr / local / lib中的lua文件,找出它,并使用-L添加库路径,使用-l链接库。

最后,在make步骤之后,如果程序无法运行,你还应该使用ln -s添加一个与/ usr / lib中的.so相关的符号链接。