本文主要是介绍tolua++ 1.0.93集成C/C++和Lua代码的工具,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
lua include object string delete reference

项目地址: 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的命令行参数如下:
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. 0, 0. 0, 0. 0); else drawObject(myLine, 1. 0, 1. 0, 1. 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
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代码的工具的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!