sudo apt-get install luajit libluajit-5.1-dev
extern "C" {
#include "luajit.h"
#include "lualib.h"
#include "lauxlib.h"
}
int main(int argc, char** argv) {
lua_State *L = luaL_newstate(); // 创建一个新的Lua环境
luaL_openlibs(L); // 打开Lua标准库
if (luaL_loadfile(L, "hello.lua") || lua_pcall(L, 0, 0, 0)) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
return 1;
}
lua_getglobal(L, "hello"); // 获取Lua中的函数
lua_pushstring(L, "World"); // 将参数压入栈中
if (lua_pcall(L, 1, 0, 0) != 0) {
fprintf(stderr, "Error: %s\n", lua_tostring(L, -1));
return 1;
}
lua_close(L); // 关闭Lua环境
return 0;
}
g++ -o hello_cpp hello.cpp -I/usr/include/luajit-2.0 -lluajit-5.1
|