vc下tolua++的使用

tolua++-1.0.93目前还尚不支持lua5.2,只能与lua5.1配合了(google code上有lua for windows,安装后再设置一下vc的include和lib目录即可)。

然后再开始编译tolua,新建一个静态库的工程,src/lib目录下的文件拖进去,OK。

tolua++.exe可以直接在网上下载,反正我自己懒得编译了(src/bin)。它的作用就是根据我们编写的pkg文件,生成一个cpp文件,我们再将这个生成的cpp文件拉进我们的工程一起编译。

 

来个Demo:

1. 先写个c++类:

// hello.h
#include <iostream> class hello { public: void say_hello() { std::cout<<"hello world"<<std::endl; } };

 

2. 编写对应的hello.pkg:

class hello
{
    hello();
    ~hello();

    void say_hello();
};

 

3. 使用tolua++生成pkg对应的cpp包装文件:

tolua++ -n hello -o hello_gen.cpp hello.pkg

(注意,我们还需在hello_gen.cpp中手动加入"#include hello.h"

 

4.  将生成的cpp加入工程,再加几行代码试验下:

int _tmain(int argc, _TCHAR* argv[])
{
    int  tolua_hello_open (lua_State*);
    
    lua_State* L= lua_open();
    luaL_openlibs(L);

    tolua_hello_open(L);
    luaL_dofile(L, "callhello.lua");

    lua_close(L);
    system("pause");
    return 0;
}

 

5. 再写个简单的lua:

-- callhello.lua
local hi= hello()
hi:say_hello()

print("fuck")

 

posted @ 2013-09-22 20:24  avexer  阅读(406)  评论(0)    收藏  举报