2013-12-08 15 views
3

test.c的:如何建立C程​​序和Lua

#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 

任何人都知道如何解決這個錯誤?謝謝。

+3

構建示例時,您需要添加導入庫或'lua * .so'共享對象。未定義的引用來自鏈接器不知道這些lua C函數在哪裏。我不確定這會在你的Linux設置上。也許試試'which lua51.so'或者查看你的bin目錄。在'test.c'之前移動你的'-I'參數,並且應該修復警告。 – greatwolf

回答

5

這是luaL_newstate,而不是luaL_newState

您還需要在命令行末尾使用-llua -lm

+0

謝謝你的幫助 – Foredoomed

0

當你創建它時,你應該在lua的.so文件中找到它,並找到它,並使用-L來添加庫路徑,使用-l來鏈接圖書館。

最後,在make步驟之後,如果程序無法運行,您還應該使用ln -s添加與/ usr/lib中的.so相關的符號鏈接。