tolua++ 1.0.93集成C/C++和Lua代码的工具
来源:互联网 发布:通用域名查询 编辑:程序博客网 时间:2024/05/17 01:06
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
当前版本: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的命令行参数如下:
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.
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文件
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);
#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中,我们可以这样子访问
2
3
4
5
6
7
8
9
...
myLine = createObject(LINE)
...
if isSelected(myLine) == TRUE then
drawObject(myLine, 1.0, 0.0, 0.0);
else
drawObject(myLine, 1.0, 1.0, 1.0);
end
...
myLine = createObject(LINE)
...
if isSelected(myLine) == TRUE then
drawObject(myLine, 1.0, 0.0, 0.0);
else
drawObject(myLine, 1.0, 1.0, 1.0);
end
...
创建一个类C++头文件的package文件
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);
};
#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中,我们可以这样子访问
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()
...
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文件
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;
};
#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文件
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;
}
CTest::CTest(void)
{
}
CTest::~CTest(void)
{
}
void CTest::SetA( int _a )
{
a = _a;
}
void CTest::SetB( std::string _b )
{
b = _b;
}
新建Test.pkg文件
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();
};
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文件如下:
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;
}
#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
运行结果如下图所示:
2
3
4
5
6
mytest = CTest:new()
mytest:SetA(5)
mytest:SetB("Hello")
print(mytest:GetA())
print(mytest:GetB())
mytest:delete()
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
3.toLua:简洁的使用说明 http://blog.csdn.net/hsz8250/article/details/755104
4.tolua++初探(三) http://blog.csdn.net/foruok/article/details/2303070
5.tolua++初探 http://wenku.baidu.com/view/444dcc2c2af90242a895e540.html- tolua++ 1.0.93集成C/C++和Lua代码的工具
- tolua++ 1.0.93集成C/C++和Lua代码的工具
- Lua调用C函数:tolua++ 的使用
- 使用tolua++创建基于C/C++语言的lua脚本
- 使用tolua++创建基于C\C++语言的lua脚本
- lua和tolua++的安装
- lua和tolua++的安装
- lua和tolua++的安装
- lua和tolua++的安装
- Lua 语言和C集成调研小结
- tolua++绑定C/C++语言函数供lua调用
- 调试集成 Java 和 C/C++ 的代码
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- lua和c的交互
- 基础总结篇之六:ContentProvider之读写联系人 (liuhe688)
- java的System.getProperty()方法可以获取的值
- CCNP-BGP/MPLS VPN配置实验
- ABAP 字符串转日期函数
- 若你安好,便是晴天
- tolua++ 1.0.93集成C/C++和Lua代码的工具
- error C2065: '_lpw ' : undeclared identifier
- CCIE-思科快速转发机制(CEF-Cisco Express Forwarding)
- redis学习心得之二【redis主从配置】
- 查找linux系统下的端口被占用进程的两种方法
- 指针的指针使用
- [转载]我一定是疯了
- 装箱问题(01背包)
- java Runtime类