1:安装luaBridge
git地址 https://github.com/vinniefalco/LuaBridge.git
2:编写程序
#include <lua.hpp> #include <LuaBridge/LuaBridge.h> #include <iostream> #include <string> class A { public: void action() { std::cout<<"Hello I am A\n"; } virtual void doPrint(int a,int b) { std::cout<<"in A a:"<<a<<"b:"<<b<<std::endl; } std::string goodMan() const { return "goodman"; } }; class B : public A { public: void hello(const std::string& info) const { std::cout<<"hello:"<<info<<std::endl; } virtual void doPrint(int a, int b) override { std::cout<<"in B just"<<(a + b) <<std::endl; } }; void globalFunction() { std::cout<<"hello this is a global func\n"; } bool reloadLuaScript(lua_State* L, const std::string& luafile) { int state = luaL_dofile(L, luafile.c_str()); if(state != LUA_OK) { return false; } return true; } int main(int argc, char** argv) { lua_State* L = luaL_newstate(); luaL_openlibs(L); std::cout<<"try load file"<<argv[1]<<std::endl; auto ok = reloadLuaScript(L, argv[1]); if(!ok) { std::cout<<"load lua file failed\n"; } else { } lua_close(L); L = nullptr; }
3:编译程序
g++ -std=c++11 -o testlua testLua.cpp -llua -ldl
4:编写Lua文件
//abc.lua print("hello"); print("This is myWorld!\n");
5:运行
./testlua abc.lua
运行结果:
try load fileabc.lua hello This is myWorld!
作者:长风
Email:844064492@qq.com
QQ群:607717453
Git:https://github.com/zhaohu19910409Dz
开源项目:https://github.com/OriginMEK/MEK
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
感谢您的阅读。如果觉得有用的就请各位大神高抬贵手“推荐一下”吧!你的精神支持是博主强大的写作动力。
如果觉得我的博客有意思,欢迎点击首页左上角的“+加关注”按钮关注我!