<a href="/2015/11/11/lua_cpp_bind/" rel="next" title="C++与Lua本质原始交互API">
            <i class="fa fa-chevron-left"></i> 
            <p class="post-nav-pre-next-title">
              C++与Lua本质原始交互API
            </p> 
          </a>
        
      </div>

      <span class="post-nav-divider"></span>

      <div class="post-nav-prev post-nav-item">
        
          <a href="/2015/11/11/lua_pairs_ipairs/" rel="prev" title="Lua中ipairs和pairs的区别与使用">
          <p class="post-nav-pre-next-title">
              Lua中ipairs和pairs的区别与使用
          </p> 
          <i class="fa fa-chevron-right"></i>
          </a>
        
      </div>
    </div>
  

  
  

  
  

  
    <p>我们用一个例子来说明.</p>

本文环境为 :

. . .

1. git clone git@github.com:LuaDist/toluapp.git
2. sudo apt-get install scons
3. cd toluapp/

4. vi custom.py 然后添加内容 :

custom.py
1
2
3
4
5
6
7
8
9
10

CCFLAGS = ['-I/usr/include/lua5.1', '-O2', '-ansi']
# 自己通过命令 sudo find / -name "*liblua*" 来查静态库.a文件在哪, 然后把路径填到下面
LIBPATH = ['/usr/lib/x86_64-linux-gnu']
LIBS = ['lua5.1', 'dl', 'm']
#prefix = '/mingw'
#build_dev=1
tolua_bin = 'tolua++5.1'
tolua_lib = 'tolua++5.1'
TOLUAPP = 'tolua++5.1'

5. scons all
6. scons install

测试

用以下五个文件测试, 输入命令 :

1. tolua++5.1 -o lua_Student.cpp Student.pkg
2. g++ *.cpp -I/usr/include/lua5.1 -llua5.1 -lm -ltolua++5.1
3. 如果执行 ./a.out 之后, 打印结果如下则为环境全部安装成功 :

1
2
3
4
5
6
7
8
9
10
Student Run
Student Run10
1
2
3
4
5
6
7
8
9
10
Student Run
Student Run10
1
2
3
4
5
6
7
8
9
10
Student Run
Student Run10
...

五个测试文件

Student.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


#include<iostream>
using namespace std;

class
{
public:
Student();
~Student();

void Run();

void Run(int a);
};
Student.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "Student.h"


Student::Student()
{
}

void Student::Run()
{
cout << "Student Run" << endl;
}

void Student::Run(int a)
{
cout << "Student Run" <<a<< endl;
}

Student::~Student()
{
}
1
2
3
4
5
6
7
8
9
10
11
$#include"Student.h"

class Student
{
public:
Student();
~Student();

void Run();
void Run @ Run2(int a);
};
test.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

studentB=Student:new() --实例化Student全局对象

function Run()
studentB:Run();
end

function Run2(a)
studentB:Run2(a);
end

function show()
local b = {}
local index

for index = 1,10,1 do
print(index)
end
end

show()

Run()

Run2(10)
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <stdio.h>
#include <unistd.h>

extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "luaconf.h"
}
#include "tolua++.h"
#include"Student.h"

extern int tolua_Student_open(lua_State* tolua_S);

int main(int argc, char* argv[])
{

while(1)
{
sleep(2);

lua_State* L = luaL_newstate();
luaL_openlibs(L);

tolua_Student_open(L);

luaL_dofile(L, "./test.lua");
lua_close(L);
}
return 0;
}

在运行的时候把test.lua文件的Run2(10) 改为 Run2(99) 之后,
打印也会跟着变为 :

1
2
3
4
5
6
7
8
9
10
Student Run
Student Run10
1
2
3
4
5
6
7
8
9
10
Student Run
Student Run99
1
2
3
4
5
6
7
8
9
10
Student Run
Student Run99
...