您的位置:首页 > 编程语言 > Lua

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

2015-03-30 23:12 686 查看
http://blog.csdn.net/ym012/article/details/7191932

//简单C++调用lua函数,改自网络
//test.cpp

[cpp] view
plaincopy

#include <stdio.h>

extern "C" {

#include "lua.h"

#include "lualib.h"

#include "lauxlib.h"

}

/*Lua解释器指针*/

lua_State* L;

int main ( int argc, char *argv[] ){

/*初始化Lua*/

L = luaL_newstate();

/*载入Lua基本库*/

luaL_openlibs(L);

/*加载lua脚本*/

luaL_dofile(L, "test.lua");

int iError = lua_pcall(L, 0, 0, 0);

if (iError)

{

lua_close(L);

return 1;

}

/*调用lua中的函数sum*/

int a = 11 ;

int b = 12 ;

lua_getglobal(L,"add");

lua_pushinteger(L,a) ;

lua_pushinteger(L,b) ;

int ret = lua_pcall(L,2,1,0) ;

if ( ret != 0 )

return 0;

printf("sum:%d + %d = %ld\n",a,b,lua_tointeger(L,-1)) ;

lua_pop(L,1);

/* 清除Lua */

lua_close(L);

return 0;

}

//test.cpp end

//test.lua

[cpp] view
plaincopy

--变量定义

width=1 ;

height=2 ;

--lua函数定义,实现加法

function sum(a,b)

return a+b ;

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
plaincopy

#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 ;

}

//end test.c

//func.lua

[python] view
plaincopy

--变量定义

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

另外两个参考例子
lua与c++的相互调用

在Lua中调用C++函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: