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!

  

 

原文地址:https://www.cnblogs.com/zhaohu/p/9406524.html