Lua入門

   最近兩天沒啥事,在研究一個開源遊戲,發現其中用了Lua腳本語言,這個東西曆來沒接觸過,因此在網上找了些個入門的小例子學習,可是過程當中出現了許多的錯誤。ide

首先在網上讀了一篇入門教程,有個例子但是卻編譯不過。函數

開發環境:OS:CentOS5.3 32位學習

          Lua 5.2this

代碼以下:lua

文件 e12.luaspa

  
  
  
  
  1. -- add two numbers 
  2. function add ( x, y ) 
  3.   return x + y 
  4. end 

 

文件 e13.cpp教程

  
  
  
  
  1. #include <stdio.h> 
  2. extern "C" 
  3.     #include "lua.h"    
  4.     #include "lualib.h"    
  5.     #include "lauxlib.h"    
  6. }    
  7.  
  8. lua_State* L;    
  9. int luaadd ( int x, int y )    
  10. {    
  11.     int sum;    
  12.     lua_getglobal(L, "add");    
  13.     lua_pushnumber(L, x);    
  14.     lua_pushnumber(L, y);    
  15.     lua_call(L, 2, 1);    
  16.     sum = (int)lua_tonumber(L, -1);    
  17.     lua_pop(L, 1);    
  18.     
  19.     return sum;    
  20. }    
  21. int main ( int argc, char *argv[] )    
  22. {    
  23.     int sum;    
  24.     L = lua_open();    
  25.     lua_baselibopen(L);    
  26.     lua_dofile(L, "e12.lua");    
  27.     sum = luaadd( 10, 15 );    
  28.     printf( "The sum is %d\n", sum );    
  29.  
  30.     lua_close(L);    
  31.     
  32.     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

在參看了網上其餘的例子之後將調用函數修改成:

  
  
  
  
  1. L = lua_open(); // luaL_newstate();  
  2. lua_baselibopen(L); //  luaL_openlibs(L);  
  3. 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

相關文章
相關標籤/搜索