Ai
1 Star 0 Fork 0

wang55www/cppexam

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
lua_test.cpp 754 Bytes
一键复制 编辑 原始数据 按行查看 历史
杨藩 提交于 2022-04-25 11:52 +08:00 . 增加lua调用c++
extern "C" {
#include "include/lua.h"
#include "include/lauxlib.h"
#include "include/lualib.h"
}
#include <iostream>
/*
* 编译样例
* g++ lua_test.cpp -o lua_test.exe -L/usr/local/lib -llua -ldl -I/usr/local
* */
bool run(lua_State* L, std::string in_data) {
lua_getglobal(L, "interpreter");
lua_pushstring(L, in_data.c_str());
lua_call(L, 1, 1);
return lua_toboolean(L, -1);
}
int main(int argc, char* argv[]) {
lua_State* L = luaL_newstate(); // 初始化
luaL_openlibs(L); //
if (luaL_loadfile(L, argv[1]) || lua_pcall(L, 0, 0, 0)) {
std::cout << "error: " << lua_tostring(L, -1) << std::endl;
return 0;
}
std::cout << run(L, "print('123123') return 2 < 3 and 4 < 6") << std::endl;
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/wang55_www/cppexam.git
git@gitee.com:wang55_www/cppexam.git
wang55_www
cppexam
cppexam
master

搜索帮助