0x00 开发环境
编译环境:ubuntu 16.04
lua版本:5.2
0x01 吐槽
很多哥们给出的代码都是随便一写,这里进行了一层简单的封装,希望可以帮到有需要的网友。
这里先给出lua和主函数的实现,c调用lua的封装函数放在这里 lua_func.hpp
0x02 lua和main.cpp
#include "lua_func.hpp"
int main(int argc, char* argv[])
{
lua_State* L = LuaFileHandle("hi.lua");
bool funcStatus = false;
if(NULL == L)
{
printf("没有创建成功! LuaFileHandle() \n");
exit(1);
}
//调用无参无返回值的lua函数
funcStatus = CallNonParmNonRetFunc(L,"showinfo");
if(false == funcStatus)
{
printf("CallNonParmNonRet 执行失败!\n");
exit(1);
}
//调用有行参数无返回值的lua函数
funcStatus = CallNonRet(L,"showstr", "guazi-reacher");
if(false == funcStatus)
{
printf("CallNonRet 执行失败!\n");
exit(1);
}
//调用有形参有返回值的lua函数
list<string> parm_datas;
parm_datas.push_back( "1" );
parm_datas.push_back( "2" );
string func_ret = CallFunc(L, "add", parm_datas);
cout << atoi(func_ret.c_str()) << endl;
/* 清除Lua */
CloseHandle(L);
return 0;
}
function showinfo()
print("welcome to lua world 无参-无返回值的函数")
end
function showstr(str)
print("The string you input is " .. str)
end
function add(x,y)
return x+y;
end