lua開發

1:安裝luaBridgeios

  

git地址  https://github.com/vinniefalco/LuaBridge.git

  

2:編寫程序c++

  

#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:編譯程序git

  

g++ -std=c++11 -o testlua testLua.cpp -llua -ldl

  

4:編寫Lua文件github

  

//abc.lua
print("hello");
print("This is myWorld!\n");

  

5:運行ide

  

./testlua abc.lua

  

  運行結果:     this

try load fileabc.lua
hello
This is myWorld!
相關文章
相關標籤/搜索