最近公司需要做向日葵远程控制软件跨平台项目,为了代码的可复用性,需嵌入跨平台脚本语言,我们选择了Lua,理由是Lua由标准C编写而成,几乎在所有操作系统和平台上都可以编译,Lua脚本可以很容易的被C/C++ 代码调用,也可以反过来调用C/C++的函数,今天就跟大家分享下c++与Lua交互的一些问题。
为了方便c++和lua的交互,我引进了LuaBridge。因为它源码简单易用,只有头文件,没有.cpp文件,不需要编译,只需要引用头文件即可,方便快捷。
下面我用vs2008工具写了一个win32控制台项目例子来讲解下c++和Lua简单的互调。
一、准备工作
从lua官网上下载最新的http://www.lua.org/download.html,下个最新的lua-5.3.4.tar.gz解压出来后可以直接拿源码编译成lib或者直接加入到项目中.我是直接把它放到项目中,编译的时候,会出现多个main函数入口的错误(移除对应的文件)。
下载LuaBridge,下载地址:https://github.com/vinniefalco/LuaBridge列表下的Source/LuaBridge是源码,Manual.html是帮助文档。
创建win32控制台项目test_lua把lua源码和LuaBridge头文件放入到项目目录下并添加到项目工程中。
二、编写代码
1、先引用Lua和LuaBridge头文件如下:
extern "C"{ #include "lua.h" #include "lauxlib.h" #include "lualib.h"}#include "LuaBridge\LuaBridge.h"
2、然后创建test_lua类和一个子类代码如下:
class test_lua{public: test_lua() { m_test_string = "c++ test string"; } ~test_lua() { } void test(int a,int b) { printf("c++ test function %d+%d=%d\n", a, b, a+b); } void SetName(std::string name) { m_name = name; } std::string GetName() const { return m_name; } int cFunc(lua_State* L) { lua_pushstring(L,"str1"); lua_pushstring(L,"str2"); return 2; } std::string m_test_string; std::string m_name; static int m_static_data;};int test_lua::m_static_data;class test_lua_child :public test_lua{ public: test_lua_child(std::string test) :m_test_child_string(test) { printf("call test_lua_child constructor\n"); } ~test_lua_child() { } std::string m_test_child_string;};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
3、创建一个lua脚本文件a.lua内容为:
print("lua script") print(test_lua.test_str)test.test_lua.static_data=12print("static_data: "..test.test_lua.static_data)test_lua.name="name_property";print("name: "..test_lua.name);test_lua:test(3,4)local ret1,ret2 = test_lua:cFunc()print("ret1="..ret1.." ret2="..ret2)local test_lua_child = test.test_lua_child("test_string")print("child string:"..test_lua_child.test_child_string);test_lua_child.name="child_name_property";print("name:"..test_lua_child.name);function lua_add_function(a,b) print("lua_add_function") return a+b;endfunction lua_add_str_function(a,b) print("lua_add_str_function") return a..b;end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
4、主函数编写
4.1、Lua的初始化和加载Lua的基本库
//初始化Lua (最后记得调用lua_close(lua_state)释放) lua_State* lua_state = luaL_newstate() //加载Lua基本库 luaL_openlibs(lua_state)
4.2、用luabridge注册到lua中
在这里要注意的是多个类注册需要加一个namespace(test),且.endClass()后面不加分号
luabridge::getGlobalNamespace(lua_state) .beginNamespace("test") .beginClass<test_lua>("test_lua") .addConstructor<void (*) (void)> () .addData("test_str",&test_lua::m_test_string) .addStaticData("static_data", &test_lua::m_static_data) .addFunction("test", &test_lua::test) .addProperty("name",&test_lua::GetName,&test_lua::SetName) .addCFunction("cFunc",&test_lua::cFunc) .endClass() .deriveClass<test_lua_child, test_lua> ("test_lua_child") .addConstructor<void (*) (std::string)> () .addData("test_child_string", &test_lua_child::m_test_child_string) .endClass() .endNamespace();test_lua test; luabridge::setGlobal(lua_state, &test, "test_lua");
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
注:test_lua也可以在lua创建,因为构造函数也注册到lua,如test.test_lua(),上面的a.lua脚本有子类test_lua_child的创建和调用其父类的属性方法
4.3、注册完成后,再返回看上面写的a.lua脚本就知道每个调用的意义,添加运行Lua脚本 代码然后执行,代码如下:
luaL_dofile(lua_state, "a.lua");lua_close(lua_state);编译执行结果为:lua scriptc++ test stringstatic_data: 12name: name_propertyc++ test function 3+4=7ret1=str1 ret2=str2call test_lua_child constructorchild string:test_stringname:child_name_property
4.4、c++调用lua方法,因为lua方法函数参数一样而且都是一个返回值,为了方便,采用模板形式(以两个参数为例)第一个参数(lua对象)和第二个参数(方法名)类型固定,后面参数用模板
template<typename R, typename T1, typename T2>
R call(lua_State* lua_state,const char* name, T1 arg1, T2 arg2){//读取方法名 lua_getglobal(lua_state, name)//判断是不是方法 if (lua_isfunction(lua_state, -1)) {//压入参数 luabridge::Stack<T1>::push(lua_state, arg1) luabridge::Stack<T2>::push(lua_state, arg2)//执行函数(参数为lua对象、参数个数,返回值个数,出错返回) lua_pcall(lua_state, 2, 1, 0) }//获取返回值 return luabridge::Stack<R>::get(lua_state, -1)}在运行lua脚本后面再加上如下调用代码://调用lua方法lua_add_function int ret = call<int>(lua_state,"lua_add_function", 5, 6) //调用lua方法lua_add_str_function std::string value = call<const char*>(lua_state,"lua_add_str_function","5", "6") printf("lua_add_function result:%d\n", ret) printf("lua_add_str_function result:%s\n", value.c_str())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
编译执行结果为:
lua scriptc++ test stringstatic_data: 12name: name_propertyc++ test function 3+4=7ret1=str1 ret2=str2call test_lua_child constructorchild string:test_stringname:child_name_propertylua_add_functionlua_add_str_functionlua_add_function result:11lua_add_str_function result:56
4.5、最后讲一下luaL_dostring
luaL_dostring跟luaL_dofile是一个作用,都是加载并运行lua脚本,只是对象不一样,看方法名就知道是一个是加载文件,另外一个是加载string,最后运行里面的lua脚本,luaL_dostring在lua嵌入到其他的脚本语言中经常用到,现在沿用上面的例子在lua_close之前加段代码简单说明下:
//定义lua脚本,调用test_lua类里的属性name并打印出来std::string lua_string = "print(\"run lua string test_lua name:\"..test_lua.name)"//加载string并运行luaL_dostring(lua_state, lua_string.c_str())编译运行得到的结果为:run lua string test_lua name:name_property
说到嵌入问题,现在做的向日葵远程控制软件的界面用的是xml,这里就涉及到lua嵌入到xml中,由于lua特性,在其他的系统这些xml都可以用,所以以后再也不用担心加个新界面每个系统还得重新再搞一套,lua嵌入xml中原理就是把lua脚本加入到一个节点中如:
<script><![CDATA[--lua代码print("run lua script") ]]></script>
解析xml对应的script节点内容,然后用luaL_dostring去加载运行就可以了。