简单C++调用lua函数及C++和lua相互函数调用小例子

//简单C++调用lua函数,改自网络
//test.cpp
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. extern "C" {  
  3. #include "lua.h"  
  4. #include "lualib.h"  
  5. #include "lauxlib.h"  
  6. }  
  7. /*Lua解释器指针*/  
  8. lua_State* L;  
  9. int main ( int argc, char *argv[] ){  
  10.     /*初始化Lua*/  
  11. L = luaL_newstate();  
  12.   /*载入Lua基本库*/  
  13.   luaL_openlibs(L);  
  14.   /*加载lua脚本*/  
  15.   luaL_dofile(L, "test.lua");  
  16.   int iError = lua_pcall(L, 0, 0, 0);  
  17.     if (iError)  
  18.     {  
  19.         lua_close(L);  
  20.         return 1;  
  21.     }  
  22.     /*调用lua中的函数sum*/  
  23.     int a = 11 ;  
  24.     int b = 12 ;  
  25.     lua_getglobal(L,"add");               
  26.     lua_pushinteger(L,a) ;  
  27.     lua_pushinteger(L,b) ;  
  28.     int ret = lua_pcall(L,2,1,0) ;  
  29.     if ( ret != 0 )  
  30.       return 0;  
  31.     printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(L,-1)) ;  
  32.     lua_pop(L,1);  
  33.     /* 清除Lua */  
  34.   lua_close(L);  
  35.   return 0;  
  36.  }  
 //test.cpp end
 //test.lua
[cpp]  view plain copy
  1.  --变量定义  
  2. width=1 ;  
  3. height=2 ;  
  4. --lua函数定义,实现加法  
  5. function sum(a,b)  
  6.     return a+b ;  
  7. end  
//test.lua end 
 很简单的程序,注意以下两个函数
 int luaL_dofile (lua_State *L, const char *filename)和int luaL_loadfile (lua_State *L, const char *filename);
 luaL_dofile在源码中的定义如下:
#define luaL_dofile(L, fn) \
(luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
 所以,如果调用luaL_loadfile 加载lua脚本文件,则不需要再次调用lua_pcall(L, 0, 0, 0);如果是调用luaL_dofile加载,则需要在程序中调用一次lua_pcall(L, 0, 0, 0);

C和lua相互调用的例子(来自网络)
//test.c
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <errno.h>  
  5. //lua头文件  
  6. #include <lua.h>  
  7. #include <lualib.h>  
  8. #include <lauxlib.h>  
  9.   
  10. #define err_exit(num,fmt,args)  \  
  11.     do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)  
  12. #define err_return(num,fmt,args)  \  
  13.     do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)  
  14.   
  15.   
  16. //lua中调用的c函数定义,实现加法  
  17. int csum(lua_State* l)  
  18. {  
  19.     int a = lua_tointeger(l,1) ;  
  20.     int b = lua_tointeger(l,2) ;  
  21.     lua_pushinteger(l,a+b) ;  
  22.     return 1 ;  
  23. }  
  24.   
  25. int main(int argc,char** argv)  
  26. {  
  27.     lua_State * l = luaL_newstate() ;        //创建lua运行环境  
  28.     if ( l == NULL ) err_return(-1,"luaL_newstat() failed");   
  29.     int ret = 0 ;  
  30.     ret = luaL_loadfile(l,"func.lua") ;      //加载lua脚本文件  
  31.     if ( ret != 0 ) err_return(-1,"luaL_loadfile failed") ;  
  32.     ret = lua_pcall(l,0,0,0) ;  
  33.     if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;  
  34.   
  35.     lua_getglobal(l,"width");              //获取lua中定义的变量  
  36.     lua_getglobal(l,"height");  
  37.     printf("height:%ld width:%ld\n",lua_tointeger(l,-1),lua_tointeger(l,-2)) ;  
  38.     lua_pop(l,1) ;                        //恢复lua的栈  
  39.   
  40.     int a = 11 ;  
  41.     int b = 12 ;  
  42.     lua_getglobal(l,"sum");               //调用lua中的函数sum  
  43.     lua_pushinteger(l,a) ;  
  44.     lua_pushinteger(l,b) ;  
  45.     ret = lua_pcall(l,2,1,0) ;  
  46.     if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;  
  47.     printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;  
  48.     lua_pop(l,1) ;  
  49.   
  50.   
  51.     const char str1[] = "hello" ;  
  52.     const char str2[] = "world" ;  
  53.     lua_getglobal(l,"mystrcat");          //调用lua中的函数mystrcat  
  54.     lua_pushstring(l,str1) ;  
  55.     lua_pushstring(l,str2) ;  
  56.     ret = lua_pcall(l,2,1,0) ;  
  57.     if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;  
  58.     printf("mystrcat:%s%s = %s\n",str1,str2,lua_tostring(l,-1)) ;  
  59.     lua_pop(l,1) ;  
  60.   
  61.   
  62.     lua_pushcfunction(l,csum) ;         //注册在lua中使用的c函数  
  63.     lua_setglobal(l,"csum") ;           //绑定到lua中的名字csum  
  64.   
  65.   
  66.     lua_getglobal(l,"mysum");           //调用lua中的mysum函数,该函数调用本程序中定义的csum函数实现加法  
  67.     lua_pushinteger(l,a) ;  
  68.     lua_pushinteger(l,b) ;  
  69.     ret = lua_pcall(l,2,1,0) ;  
  70.     if ( ret != 0 ) err_return(-1,"lua_pcall failed:%s",lua_tostring(l,-1)) ;  
  71.     printf("mysum:%d + %d = %ld\n",a,b,lua_tointeger(l,-1)) ;  
  72.     lua_pop(l,1) ;  
  73.   
  74.     lua_close(l) ;                     //释放lua运行环境  
  75.     return 0 ;  
  76. }  


//end test.c
//func.lua
[python]  view plain copy
  1. --变量定义  
  2. width=1 ;  
  3. height=2 ;  
  4. --lua函数定义,实现加法  
  5. function sum(a,b)  
  6.     return a+b ;  
  7. end  
  8. --lua函数定义,实现字符串相加  
  9. function mystrcat(a,b)  
  10.     return a..b ;  
  11. end  
  12. --lua函数定义,通过调用c代码中的csum函数实现加法  
  13. function mysum(a,b)  
  14.     return csum(a,b) ;  
  15. end  


//end func.lua
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值