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

相关文章
相关标签/搜索