最近兩天沒啥事,在研究一個開源遊戲,發現其中用了Lua腳本語言,這個東西曆來沒接觸過,因此在網上找了些個入門的小例子學習,可是過程當中出現了許多的錯誤。ide
首先在網上讀了一篇入門教程,有個例子但是卻編譯不過。函數
開發環境:OS:CentOS5.3 32位學習
Lua 5.2this
代碼以下:lua
文件 e12.luaspa
- -- add two numbers
- function add ( x, y )
- return x + y
- end
文件 e13.cpp教程
- #include <stdio.h>
- extern "C"
- {
- #include "lua.h"
- #include "lualib.h"
- #include "lauxlib.h"
- }
- lua_State* L;
- int luaadd ( int x, int y )
- {
- int sum;
- lua_getglobal(L, "add");
- lua_pushnumber(L, x);
- lua_pushnumber(L, y);
- lua_call(L, 2, 1);
- sum = (int)lua_tonumber(L, -1);
- lua_pop(L, 1);
- return sum;
- }
- int main ( int argc, char *argv[] )
- {
- int sum;
- L = lua_open();
- lua_baselibopen(L);
- lua_dofile(L, "e12.lua");
- sum = luaadd( 10, 15 );
- printf( "The sum is %d\n", sum );
- lua_close(L);
- return 0;
- }
編譯方法:遊戲
g++ e13.cpp -llua -llualib -o e13開發
而後發現出現錯誤:文檔
e13.cpp: In function ‘int main(int, char**)’:
e13.cpp:31: error: ‘lua_open’ was not declared in this scope
e13.cpp:34: error: ‘lua_baselibopen’ was not declared in this scope
e13.cpp:38: error: ‘lua_dofile’ was not declared in this scope
在參看了網上其餘的例子之後將調用函數修改成:
- L = lua_open(); // luaL_newstate();
- lua_baselibopen(L); // luaL_openlibs(L);
- lua_dofile(L, "e12.lua");// luaL_dofile(L, "e12.lua");
而後編譯出現如下錯誤:
/usr/bin/ld: cannot find -llualib
collect2: ld returned 1 exit status
又搜索了一些文檔將編譯方法修改成:
g++ e13.cpp -o e13 -I/usr/local/include /usr/local/lib/liblua.a -llua -ldl
編譯經過,執行:
./e13
The sum is 25