首先你要安装lua的dev,安装很简单:
yum install lua-devel
即可,很多Linux系统自带Lua但是没有dev,有点小坑。
下面是Lua文件,里面就两个函数:
function add(a, b)
return a + b
end
function hello()
print("Hello Lua!!!")
end
之后是cpp文件的调用代码:
#include<iostream>
#include<string>
using std::cout;
using std::endl;
using std::string;
//extern的意义,将下面文件当成C风格文件使用
extern "C"
{
#include<lua.h>
#include<lauxlib.h>
#include<lualib.h>
}
int main()
{
//创建环境
lua_State *L = luaL_newstate();
if(L == NULL)
{
cout << "State error" << endl;
return -1;
}
//加载库
luaL_openlibs(L);
const string file = "func.lua";
// 加载文件
int ret = luaL_dofile(L, file.c_str());
if(ret)
{