1.需求
最近在搞一个项目时需要在程序运行时进行其中环境的改变故采用luaplus作为动态引入,实现和c++代码之间的互调
2.解决
需要下载luaplus最新代码进行编译
3.代码
mymain.cpp
#include <iostream>
#include "LuaPlus.h"
using namespace std;
using namespace LuaPlus;
class MyTest
{
public:
int sum(int x, int y) {return x + y;}
};
int LS_PrintNumber(LuaState * state)
{
LuaStack args(state);
int ncount = args.Count();
cout<<ncount<<endl;
for (int n = 1; n<= ncount; ++n)
cout<<args[n].GetNumber()<<endl;
return 0;
}
int main()
{
LuaStateOwner state;
MyTest test;
state->GetGlobals().Register("PrintNumber", LS_PrintNumber);
state->GetGlobals().RegisterDirect("sum", test, &MyTest::sum);
int iret = state->DoFile("test.lua");
int mytest = state->GetGlobal("health").GetInteger();
cout<<mytest<<endl;
LuaFunction<float> Add = state->GetGlobal("Add");
cout<<Add(12.34, 23.45)<<endl;
cout<<state->GetGlobal("aa").GetInteger()<<endl;
std::system("pause");
return 0;
}
test.lua
health=200;
function Add(x,y)
return x+y
end
PrintNumber(10, 20);
aa=sum(1000, 2000);
4.备注
1.在win7+vs2010+win32命令行下编译通过