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 1enum { 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++.html2.tolua++參考手冊(翻譯一)tolua++使用
http://blog.csdn.net/obsona/article/details/34785183.toLua:簡潔的使用說明
http://blog.csdn.net/hsz8250/article/details/7551044.tolua++初探(三)
http://blog.csdn.net/foruok/article/details/2303070
5.tolua++初探
http://wenku.baidu.com/view/444dcc2c2af90242a895e540.html