luabind和c++相互调用
来源:互联网 发布:中国好声音网络歌手 编辑:程序博客网 时间:2024/04/26 06:25
#include "stdafx.h"#include<iostream>#include "luabind\luabind.hpp"extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}using namespace std;bool LoadScript(lua_State *L,const string& fname){ if (luaL_dofile(L,fname.c_str())) { cerr<<lua_tostring(L,-1)<<endl; return false; } return true;}void testFunc(int k){ cout<<"hello there, i am a cpp fun"<<endl; cout<<"input num:="<<k<<endl;}class NumberPrinter {public: NumberPrinter(int number) : m_number(number) {} void print() { cout << m_number << endl; }private: int m_number;};int _tmain(int argc, _TCHAR* argv[]){ using namespace luabind; lua_State* L = luaL_newstate(); module(L, "cppapi") [ def("testFunc", (void(*)(int))testFunc) ]; // 使用LuaBind导出NumberPrinter类 luaopen_base(L); luabind::open(L); luabind::module(L) [ luabind::class_<NumberPrinter>("NumberPrinter") .def(luabind::constructor<int>()) .def("print", &NumberPrinter::print) ]; luaL_openlibs(L); LoadScript(L,"test.lua"); try{ int add_ret = luabind::call_function<int>(L,"add",10,4); int call_global = luabind::object_cast<int>(luabind::globals(L)["nGlobal"]); string strGlobal = luabind::object_cast<string>(luabind::globals(L)["strGlobal"]) ; luabind::object lua_object = luabind::globals(L)["t"]; //--2种办法load table string strName = luabind::object_cast<string>(lua_object["name"]); int iAge = luabind::object_cast<int>(lua_object["age"]); string strElse = luabind::object_cast<string>(lua_object["desc"]); } catch(luabind::error& e) { cout<<e.what()<<endl; printf("AI throw error: err_msg[%s]", lua_tostring(L, -1)); return false; } lua_close(L); return 0;}
其中test.lua的代码如下:
Print2000 = NumberPrinter(2000)
Print2000:print()
nGlobal = 10 --一个全局的整形变量
strGlobal = "hello i am in lua" --一个全局的字符串变量
--一个返回值为int类型的函数
function add(a, b)
return a+b
end
--一个返回值为string类型的函数
function strEcho(a)
print(a)
return 'haha i h
ave print your input param'
end
cppapi.testFunc(10) --调用c++暴露的一个测试函数
t={name='ettan', age=23, desc='正值花季年龄'}
运行结果为:
这上面luabind调用c++函数的实例:调用testFunc函数;也有c++调用lua的代码,具体的见代码。
此代码在我的vs2010上面调试通过,前提是必须配好环境如:添加依赖库luabind.debug.lib;lua.debug.lib
添加依赖库路径。
特别注意:
首先Lua是“动态编译的脚本语言”,而loadfile只是把源文件加载到内存中,还少了“编译”这一步,可以用“luaL_dofile(L,"test.lua");”来替换,它既加载又编译。替换之后执行应该就没有问题了。
但是还没完,luaL_dofile 实际上是个宏:
- #define luaL_dofile(L, fn) \
- (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))
LUA_MULTRET也是宏定义,值为-1,表示函数有多个返回值(Lua规则,pil 24.2--堆栈)。
扩展开来就是以下两句:
- luaL_loadfile(L, fn);
- lua_pcall(L, 0, LUA_MULTRET, 0);
pcall以上述参数执行的时候,会把加载到内存中的源程序编译成可以用于执行的2进制代码,并将全局变量压栈(在Lua中,函数也是变量,pil 2.5 -- Functions,毕竟函数名和函数体是不同的2个东西)。就跟PE文件格式里的Section一样(PE文件就是Windows3.1之后的.exe/.dll文件)。当然如果你不知道什么PE文件也没关系--我只是打个比方--就当成VS2005编译代码时生成的.obj文件。
虽然实际使用中99%的情况都是直接使用dofile,但是我想将该问题提出来说可以更加直观的理解“动态编译”。
参照:
代码和文字大部分出自 1. http://blog.csdn.net/caoyanting007/article/details/5709820
2. http://mobile.51cto.com/iphone-285654.htm
- luabind和c++相互调用
- C和C++相互调用
- C和C++相互调用
- C和C++相互调用
- OC和C相互调用
- C和C++相互调用
- C和C++相互调用
- C和C++相互调用
- 汇编和C相互调用
- C和C++相互调用
- C和C++相互调用
- C和C++相互调用
- c和c++相互调用
- asm和c的相互调用
- c和C++代码相互调用
- C函数和C++函数相互调用
- C 和C++ 之间的相互调用
- JAVA和C/C++相互调用
- 在standalone-cluster模式上运行spark应用程序(用sbt打包)
- trac 报错:Cannot load Python bindings for MySQL
- json数组对象和对象数组
- 数据结构之链表(C实现)
- 条件注释判断浏览器<!--[if !IE]><!--[if IE]><!--[if lt IE 6]><!--[if gte IE 6]>
- luabind和c++相互调用
- IOS开发之所有类型的UIKeyboardType图片展示
- DSP C语言优化-(二)
- 使用迭代器遍历图像
- VC6.0各个小模块代码整理集合
- jQuery解读之after
- 创建对象和使用方法以及属性
- java 调用第三方dll学习心得
- 2014编程之美资格赛第三题-格格取数