tolua++ 1.0.93集成C/C++和Lua代码的工具

tolua是一款工具,大大简化了C/C++和Lua代码的集成。基于干净的头文件(或从真正的头文件提取),tolua自动生成绑定代码供lua访问C/C++的功能。使用Lua API和标记方法设施(tag method facilities),tolua可以映射C/C++的常量,外部变量,函数,类和方法到Lua。 tolua++是tolua的一个扩展版本,一款集成C/C++和Lua代码的工具。
项目地址: http://www.codenix.com/~tolua/
当前版本:1.0.93
 
编译:
打开在文件夹" ...\tolua++-1.0.93\win32\vc7"下的解决方案" toluapp.sln",设置项目的"附加包含目录",添加"...\Lua\5.1\include"(Lua 5.1头文件目录),设置"附加库目录",添加"...\Lua\5.1\lib",编译项目的" withLua51_Release"配置,在"...\tolua++-1.0.93\bin"目录下生成" tolua++.exe"文件。
 
tolua++.exe的命令行参数如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
usage: tolua++ [options] input_file

Command line options are:
  -v       : print version information.
  -o  file : set output file; default is stdout.
  -H  file : create include file.
  -n  name : set package name; default is input file root name.
  -p       : parse only.
  -P       : parse and print structure information (for debug).
  -S       : disable support for c++ strings.
  -1       : substract 1 to operator[] index (for compatibility with tolua5).
  -L  file : run lua file (with dofile()) before doing anything.
  -D       : disable automatic exporting of destructors for classes that have constructors (for compatibility with tolua5)
  -W       : disable warnings for unsupported features (for compatibility with tolua5)
  -C       : disable cleanup of included lua code (for easier debugging)
  -E  value[=value] : add extra values to the luastate
  -t       : export a list of types asociates with the C++ typeid name
  -q       : don't print warnings to the console
  -h       : print this message.
Should the input file be omitted, stdin is assumed;
in that case, the package name must be explicitly set.
 
创建一个静态库工程,添加"...\tolua++-1.0.93\src\lib"下的文件到工程,设置项目的"附加包含目录",添加"...\tolua++-1.0.93\include",添加"...\Lua\5.1\include",编译运行生成" tolua++.lib"文件。
 
在使用tolua之前,我们需要创建一个package文件,一个干净的C/C++头文件,只列出所有我们希望暴露给lua使用的常数,变量,函数,类和方法。然后tolua剖析这个文件,并自动创建C/C++文件,该文件会自动把C/C++的代码绑定到Lua。当我们的程序链接到这个文件时,我们就能从Lua访问对应的C/C++代码。一个package文件可以包括常规头文件,其他package文件,以及lua文件。
示例:
创建一个类C头文件的package文件
1
2
3
4
5
6
7
8
9
10
11
#define FALSE  0
#define TRUE  1

enum { 
 POINT =  100
 LINE, 
 POLYGON
}
Object* createObejct ( int type);
void drawObject (Object* obj,  double red,  double green,  double blue);
int isSelected (Object* obj);
在Lua中,我们可以这样子访问
1
2
3
4
5
6
7
8
9
...
myLine = createObject(LINE)
...
if isSelected(myLine) == TRUE  then
  drawObject(myLine,  1. 00. 00. 0);
else
  drawObject(myLine,  1. 01. 01. 0);
end
...
创建一个类C++头文件的package文件
1
2
3
4
5
6
7
8
9
10
11
12
13
#define FALSE  0
#define TRUE  1
class Shape
{
   void draw ( void);
   void draw ( double red,  double green,  double blue);
   int isSelected ( void);
};
class Line :  public Shape
{
 Line ( double x1,  double y1,  double x2,  double y2);
 ~Line ( void);
};
在Lua中,我们可以这样子访问
1
2
3
4
5
6
7
8
9
10
11
...
myLine = Line:new ( 0, 0, 1, 1)
...
if myLine:isSelected() == TRUE  then
 myLine:draw( 1. 0, 0. 0, 0. 0)
else
 myLine:draw()
end
...
myLine:delete()
...
 
下面开始一个完整示例:
新建Test.h文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#pragma once
#include <string>

class CTest
{
public:
    CTest( void);
    ~CTest( void);
    
     void SetA( int _a);
     int GetA(){ return a;}

     void SetB(std::string _b);
    std::string GetB(){ return b;}
private:
     int a;
    std::string b;
};
新建Test.cpp文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include  "Test.h"

CTest::CTest( void)
{
}

CTest::~CTest( void)
{
}

void CTest::SetA(  int _a )
{
    a = _a;
}

void CTest::SetB( std::string _b )
{
    b = _b;
}
新建Test.pkg文件
1
2
3
4
5
6
7
8
9
10
11
12
13
$#include  "Test.h"

class CTest
{
    CTest( void);
    ~CTest( void);
    
     void SetA( int _a);
     int GetA();

     void SetB(std::string _b);
    std::string GetB();
};
在命令行输入:
1
tolua++ -o TestLua.cpp Test.pkg
将会生成 TestLua.cpp文件。建立一个控制台工程,设置项目的"附加包含目录",添加"...\tolua++-1.0.93\include",添加"...\Lua\5.1\include",设置"附加库目录",添加"...\Lua\5.1\lib",添加"...\tolua++-1.0.93\lib",把"Test.h"、"Test.cpp"、"TestLua.cpp"加到工程中,编写 main.cpp文件如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include  "lua.hpp"
#pragma comment(lib,  "lua5.1.lib")
#pragma comment(lib,  "tolua++.lib")

int  tolua_Test_open (lua_State* tolua_S);

int main()
{
    lua_State *L = luaL_newstate(); 
    luaopen_base(L); 
    tolua_Test_open(L);
    luaL_dofile(L,  "testtest.lua"); 
    lua_close(L); 
     return  0;
}
新建testtest.lua文件
1
2
3
4
5
6
mytest = CTest:new()
mytest:SetA( 5)
mytest:SetB( "Hello")
print(mytest:GetA())
print(mytest:GetB())
mytest:delete()
运行结果如下图所示:

 
参考资料:
1.tolua++ - Reference Manual http://www.codenix.com/~tolua/tolua++.html
2.tolua++参考手册(翻译一)tolua++使用 http://blog.csdn.net/obsona/article/details/3478518
5.tolua++初探 http://wenku.baidu.com/view/444dcc2c2af90242a895e540.html
 
转载: 点击打开链接
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值