LuaIntf 是一个提供 C++ 与 Lua 互操作的库,项目地址:https://github.com/SteveKChiu/lua-intf。详细说明可参考作者在 GitHub 上的 README.md,描述十分清晰。

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();