lua开发
Posted zhaohu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lua开发相关的知识,希望对你有一定的参考价值。
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 "; } 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 "; } 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 "; } 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! ");
5:运行
./testlua abc.lua
运行结果:
try load fileabc.lua hello This is myWorld!
以上是关于lua开发的主要内容,如果未能解决你的问题,请参考以下文章