LuaIntf-第一步
LuaInft https://github.com/SteveKChiu/lua-intf
ReadME .MD https://github.com/SteveKChiu/lua-intf/blob/master/README.md
一些基础信息,作者在github上面描述很清楚
这个lib 提供了cpp和lua交互 解决方案
1.运行执行lua文件 函数
lua_State *l = luaL_newstate(); luaL_openlibs(l); LuaIntf:: LuaContext ctx(l); ctx.doFile("1.lua"); LuaIntf::LuaRef func(l, "func"); func(1000);
2.导出class
#include "LuaIntf/LuaIntf.h" using namespace LuaIntf; class Test { public: void Print() { cout << __FUNCTION__ << this->GetValue() << endl; } void SetValue(int v) { this->_value = v; }//setter int GetValue() { return this->_value; };//getter Test(string s) {} Test(void) {} private: int _value = 0; // inner value }; int main(int argc, char *argv[]) { lua_State *l = luaL_newstate(); luaL_openlibs(l); LuaIntf::LuaBinding(l).beginClass<Test>("Test") .addConstructor(LUA_ARGS(_opt<std::string>)) .addConstructor(LUA_ARGS()) .addFunction("Print", &Test::Print) .addProperty("v", &Test::GetValue, &Test::SetValue) // 绑定getter 和setter,lua变量名为v .endClass(); try { LuaIntf::LuaContext ctx(l); ctx.doFile("1.lua"); LuaIntf::LuaRef func(l, "func"); func(1000); } catch (LuaException e) { cout << e.what(); } _CrtDumpMemoryLeaks(); system("pause"); return 0; } lua代码 print("567856"); function func(x) print(x); end local xxx= Test(); xxx:Print(); xxx.v=3; xxx:Print();
3.注册模块
比上面的代码多了: LuaIntf::LuaBinding(l).beginModule("cpp") /// cpp 模块名字 .addFunction("log", &log1) .addFunction("FUNCNAME", [=] { log1(__FUNCTION__); }) .endModule(); lua代码 cpp.log("haha"); cpp.FUNCNAME();