lxyfirst

C++博客 首页 新随笔 联系 聚合 管理
  33 Posts :: 3 Stories :: 27 Comments :: 0 Trackbacks
lua作为小巧精悍的脚本语言,易于嵌入c/c++中 , 广泛应用于游戏AI ,实际上在任何经常变化的逻辑上都可以使用lua实现,配合c/c++实现的底层接口服务,能够大大降低系统的维护成本。下面对lua和c/c++的交互调用做一个实例分析:
lua提供了API用于在c/c++中构造lua的运行环境,相关接口如下:
//创建lua运行上下文
lua_State* luaL_newstate(void) ;
//加载lua脚本文件
int luaL_loadfile(lua_State *L, const char *filename);

lua和c/c++的数据交互通过"栈"进行 ,操作数据时,首先将数据拷贝到"栈"上,然后获取数据,栈中的每个数据通过索引值进行定位,索引值为正时表示相对于栈底的偏移索引,索引值为负时表示相对于栈顶的偏移索引,索引值以1或-1为起始值,因此栈顶索引值永远为-1 ,栈底索引值永远为1 。 "栈"相当于数据在lua和c/c++之间的中转地。每种数据都有相应的存取接口 。
数据入"栈"接口:
void  (lua_pushnil) (lua_State *L);
void  (lua_pushnumber) (lua_State *L, lua_Number n);
void  (lua_pushinteger) (lua_State *L, lua_Integer n);
void  (lua_pushlstring) (lua_State *L, const char *s, size_t l);
void  (lua_pushstring) (lua_State *L, const char *s);
void  (lua_pushboolean) (lua_State *L, int b);
void  (lua_pushcclosure) (lua_State *L, lua_CFunction fn, int n);

数据获取接口:
lua_Number      (lua_tonumber) (lua_State *L, int idx);
lua_Integer     (lua_tointeger) (lua_State *L, int idx);
int             (lua_toboolean) (lua_State *L, int idx);
const char     *(lua_tolstring) (lua_State *L, int idx, size_t *len);
lua_CFunction   (lua_tocfunction) (lua_State *L, int idx);


"栈"操作接口:
int   (lua_gettop) (lua_State *L);
void  (lua_settop) (lua_State *L, int idx);
void  (lua_pushvalue) (lua_State *L, int idx);
void  (lua_remove) (lua_State *L, int idx);
void  (lua_insert) (lua_State *L, int idx);
void  (lua_replace) (lua_State *L, int idx);
int   (lua_checkstack) (lua_State *L, int sz);

lua中定义的变量和函数存放在一个全局table中,索引值为LUA_GLOBALSINDEX ,table相关操作接口:
void  (lua_gettable) (lua_State *L, int idx);
void  (lua_getfield) (lua_State *L, int idx, const char *k);
void  (lua_settable) (lua_State *L, int idx);
void  (lua_setfield) (lua_State *L, int idx, const char *k);

当"栈"中包含执行脚本需要的所有要素(函数名和参数)后,调用lua_pcall执行脚本:
int   (lua_pcall) (lua_State *L, int nargs, int nresults, int errfunc);

下面进行实例说明:
func.lua
--变量定义
width
=1 ;
height
=2 ;
--lua函数定义,实现加法
function sum(a,b)
    
return a+b ;
end
--lua函数定义,实现字符串相加
function mystrcat(a,b)
    
return a..b ;
end
--lua函数定义,通过调用c代码中的csum函数实现加法
function mysum(a,b)
    
return csum(a,b) ;
end

test_lua.c
#include <stdio.h>
#include 
<stdlib.h>
#include 
<string.h>
#include <errno.h>
//lua头文件
#include <lua.h>
#include 
<lualib.h>
#include 
<lauxlib.h>


#define err_exit(num,fmt,args)  \
    
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)
#define err_return(num,fmt,args)  \
    
do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)

//lua中调用的c函数定义,实现加法
int csum(lua_State* l)
{
    
int a = lua_tointeger(l,1) ;
    
int b = lua_tointeger(l,2) ;
    lua_pushinteger(l,a
+b) ;
    
return 1 ;
}

int main(int argc,char** argv)
{
    lua_State 
* l = luaL_newstate() ;        //创建lua运行环境
    
if ( l == NULL ) err_return(-1,"luaL_newstat() failed"); 
    
int ret = 0 ;
    ret 
= luaL_loadfile(l,"func.lua") ;      //加载lua脚本文件
    
if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;
    ret 
= lua_pcall(l,0,0,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;

    lua_getglobal(l,
"width");              //获取lua中定义的变量
    lua_getglobal(l,
"height");
    printf(
"height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;
    lua_pop(l,
1) ;                        //恢复lua的栈

    
int a = 11 ;
    
int b = 12 ;
    lua_getglobal(l,
"sum");               //调用lua中的函数sum
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,
1) ;

    
const char str1[] = "hello" ;
    
const char str2[] = "world" ;
    lua_getglobal(l,
"mystrcat");          //调用lua中的函数mystrcat
    lua_pushstring(l,str1) ;
    lua_pushstring(l,str2) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;
    lua_pop(l,
1) ;

    lua_pushcfunction(l,csum) ;         //注册在lua中使用的c函数
    lua_setglobal(l,
"csum") ;           //绑定到lua中的名字csum

    lua_getglobal(l,
"mysum");           //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法
    lua_pushinteger(l,a) ;
    lua_pushinteger(l,b) ;
    ret 
= lua_pcall(l,2,1,0) ;
    
if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;
    printf(
"mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;
    lua_pop(l,
1) ;

    lua_close(l) ;                     //释放lua运行环境
    
return 0 ;
}



posted on 2008-10-29 14:37 star 阅读(28432) 评论(12)  编辑 收藏 引用

Feedback

# re: lua和c/c++互相调用实例分析 2008-11-01 11:00 金山词霸2008
有个疑问,C++已经足够强大,为什么还需要lua脚本语言?  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2008-11-03 18:45 diwayou
灵活性!  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2008-11-03 20:30 amuro1987218
除去图像这一层极端效率敏感的部分外,其他部分其实用效率适当下降换取灵活是值得的。  回复  更多评论
  

# re: lua和c/c++互相调用实例分析[未登录] 2008-12-19 17:00 hunter
这些都是简单数据类型的操作,能不能提供一个操作struct(里面还有struct)的例子啊。  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2009-01-06 20:34 lua
写的非常好,我折腾两三天都没有搞定.看了之后我全明白了.  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2011-11-09 14:00 无赖二号
非常好,非常感谢。  回复  更多评论
  

# re: lua和c/c++互相调用实例分析[未登录] 2012-04-24 14:56 jerome
为什么我不能编译

]# gcc testlua.C -llua -lm -ldl -I/usr/local/include/ -L/usr/local/lib/ -o testlua
/tmp/ccIkmvNG.o: In function `csum(lua_State*)':
testlua.C:(.text+0x16): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x27): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x3c): undefined reference to `lua_pushinteger(lua_State*, long)'
/tmp/ccIkmvNG.o: In function `main':
testlua.C:(.text+0x59): undefined reference to `luaL_newstate()'
testlua.C:(.text+0x8d): undefined reference to `luaL_loadfile(lua_State*, char const*)'
testlua.C:(.text+0xc4): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0xf6): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x109): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x117): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x128): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x14b): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x16c): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x17b): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x18a): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x1a2): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x1be): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x1ea): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x210): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x24b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x258): undefined reference to `lua_pushstring(lua_State*, char const*)'
testlua.C:(.text+0x265): undefined reference to `lua_pushstring(lua_State*, char const*)'
testlua.C:(.text+0x27d): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x299): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x2ca): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x2f2): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x305): undefined reference to `lua_pushcclosure(lua_State*, int (*)(lua_State*), int)'
testlua.C:(.text+0x318): undefined reference to `lua_setfield(lua_State*, int, char const*)'
testlua.C:(.text+0x32b): undefined reference to `lua_getfield(lua_State*, int, char const*)'
testlua.C:(.text+0x33a): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x349): undefined reference to `lua_pushinteger(lua_State*, long)'
testlua.C:(.text+0x361): undefined reference to `lua_pcall(lua_State*, int, int, int)'
testlua.C:(.text+0x37d): undefined reference to `lua_tolstring(lua_State*, int, unsigned long*)'
testlua.C:(.text+0x3a6): undefined reference to `lua_tointeger(lua_State*, int)'
testlua.C:(.text+0x3cc): undefined reference to `lua_settop(lua_State*, int)'
testlua.C:(.text+0x3d5): undefined reference to `lua_close(lua_State*)'
/tmp/ccIkmvNG.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
  回复  更多评论
  

# re: lua和c/c++互相调用实例分析[未登录] 2012-04-26 17:43 tommy
请看清楚了作者写的文件名是test_lua.c
如果你保存为cpp文件了,关于lua相关头文件应该使用:
extern "C"{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
否则编译会提示你的那种问题  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2012-05-03 10:20 star
@jerome
链接时找不到函数定义,这个应该是lua库的路径问题。  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2014-02-02 14:46 ixil
@hunter
point=alien.buffer
赋值
x=point:get(1,"long")
y=point:get(5,"long")  回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2014-03-13 18:50 fy
lua_pop(l,1) ; //恢复lua的栈

这一行应该改为 lua_pop(l,2) ;   回复  更多评论
  

# re: lua和c/c++互相调用实例分析 2014-11-29 13:54 iHuahua
@金山词霸2008
术业有专攻,C++那么强大,你倒是用他写个网页我看看  回复  更多评论
  


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理