tolua++ 编译 及使用 简单介绍 标签: tolua++luaC++

 

tolua++ 编译 及使用 简单介绍

标签: tolua++luaC++
2015-11-03 00:52  1576人阅读  评论(0)  收藏  举报
  分类:

tolua++ 是用来把 C++代码 生成 在Lua中使用的接口的工具,编译步骤比较繁琐。


1、下载lua5.1.4,解压后,新建空项目,修改成静态库lib ,在C++ 代码生成选项卡 中设置 运行库 为 多线程 DLL (/MD) ,然后把安全检查 设置为 禁用安全检查 (/GS-)

把lua5.1.4  src目录下的 除了 lua.c luac.c 的所有文件添加到项目中,编译。

编译成功后获得 lua5.1.4.lib 。


2、下载 tolua++ ,解压后,新建空项目,其它设置和上面一样,添加 tolua++ 文件夹中的 src/lib/ 里面的所有代码到项目中,编译成静态库。

编译成功后获得 tolua++.lib


到 tolua++ 文件夹中的 win32/vc7 中,打开 toluaapp.sln ,设置好 lua 头文件目录,把lua5.1.4.lib 添加到链接库 ,添加 tolua++ 目录的include 目录到头文件目录,把 tolua++.lib 添加到链接库,编译。

编译成功后,在tolua++目录的bin 中获得 tolua++.exe


3、编写Student 类来作为测试

Student.h

[cpp]  view plain  copy
  1. #pragma once  
  2.   
  3. #include<iostream>  
  4. using namespace std;  
  5.   
  6. class Student  
  7. {  
  8. public:  
  9.     Student();  
  10.     ~Student();  
  11.   
  12.     void Run();  
  13.   
  14.     void Run(int a);  
  15. };  

Student.cpp

[cpp]  view plain  copy
  1. #include "Student.h"  
  2.   
  3.   
  4. Student::Student()  
  5. {  
  6. }  
  7.   
  8. void Student::Run()  
  9. {  
  10.     cout << "Student Run" << endl;  
  11. }  
  12.   
  13. void Student::Run(int a)  
  14. {  
  15.     cout << "Student Run" <<a<< endl;  
  16. }  
  17.   
  18. Student::~Student()  
  19. {  
  20. }  

为Student 类创建 pkg文件

Student.pkg

[html]  view plain  copy
  1. $#include"Student.h"  
  2.   
  3. class Student  
  4. {  
  5. public:  
  6.     Student();  
  7.     ~Student();  
  8.   
  9.     void Run();  
  10.     void Run @ Run2(int a);  
  11. };  

把tolua++.exe 到 Student 的类文件目录,右键命令行

[html]  view plain  copy
  1. "tolua++.exe" -o lua_Student.cpp Student.pkg  

生成成功获得 lua_Student.cpp


4、创建测试项目在lua 脚本中使用 Student 类

创建空项目,项目中设置好 lua5.1.4 和 tolua++ 的头文件目录 ,以及引用 lua5.1.4.lib 和 tolua++.lib 。

然后把 Student 类 和生成的 lua_Student.cpp 添加到项目中。

main.cpp 

[cpp]  view plain  copy
  1. #include <stdio.h>    
  2. #include<windows.h>    
  3.   
  4. extern "C"  
  5. {  
  6.     #include "lua.h"    
  7.     #include "lualib.h"    
  8.     #include "lauxlib.h"    
  9.     #include "luaconf.h"    
  10. }  
  11. #include "tolua++.h"  
  12. #include"Student.h"  
  13.   
  14. extern int tolua_Student_open(lua_State* tolua_S);  
  15.   
  16. int main(int argc, char* argv[])  
  17. {  
  18.     lua_State* L = luaL_newstate();  
  19.     luaL_openlibs(L);  
  20.   
  21.     tolua_Student_open(L);  
  22.   
  23.     luaL_dofile(L, "./test.lua");  
  24.     lua_close(L);  
  25.   
  26.     system("pause");  
  27.     return 0;  
  28. }  


lua脚本文件 test.lua

[html]  view plain  copy
  1. studentB=Student:new() --实例化Student全局对象  
  2.   
  3. function Run()  
  4.     studentB:Run();  
  5. end  
  6.   
  7. function Run2(a)  
  8.     studentB:Run2(a);  
  9. end  
  10.   
  11. function show()    
  12.     local b = {}    
  13.     local index    
  14.         
  15.     for index = 1,10,1 do    
  16.         print(index)    
  17.     end    
  18. end    
  19.     
  20. show()    
  21.   
  22. Run()  
  23.   
  24. Run2(10)  





项目打包下载 (lua5.1.4 + luajit + toluapp)

百度网盘:

[html]  view plain  copy
  1. http://pan.baidu.com/s/1i3xvykt  


CSDN下载:

[html]  view plain  copy
  1. http://download.csdn.net/detail/cp790621656/9235459