刚开始学LUA,首先搭建LUA环境
我下的是LUA 5.2.3,http://www.lua.org/download.html
在mac下安装LUA ,进入到刚下载的LUA目录,执行下面两条命令
make macosx sudo make install然后打开终端输入: LUA
Lua 5.2.3 Copyright (C) 1994-2013 Lua.org, PUC-Rio
成功安装
在xcode里新建一个Application 里命令行工具(Commond Line Tool)
把lua-5.2.3文件拷到src目录下,在工程里就能看到LUA源代码
注意:新创建的有一个main文件,在lua源代码lua.c里也有一个main方法,把lua.c里的main改一个名字,暂时叫tempMain,这样来避免编译出错
1、从C++调用LUA的方法,没有参数也没返回值
<pre name="code" class="cpp">int main(int argc, const char * argv[]) {
//创建一个LUA环境
lua_State *lua_state;
lua_state = luaL_newstate();
static const luaL_Reg lualibs[] = {
{ "base", luaopen_base },
{ NULL , NULL}
};
const luaL_Reg *lib = lualibs;
for ( ; lib->func != NULL ; lib++) {
lib->func(lua_state);
lua_settop(lua_state, 0);
}
printf("start excite the Hello.lua file\n");
luaL_dofile(lua_state,"hello.lua");
//lua_getglobal 函数去加载到内存里的hello.lua文件里找hello主方法,并把hello方法放到LUA栈里
lua_getglobal(lua_state, "hello");
//lua_call执行栈顶的元素,这里是hello方法,第一个参数是lua栈环境,第二个参数是传给LUA的个数,这里没有参数为0,第三个参数是
//返回值个数,没返回值为0
lua_call(lua_state,0,0);
lua_close(lua_state);
return 0;
}
对应的LUA文件 hello.lua
function hello()
print("Hello Lua")
end
运行结果:
start excite the Hello.lua file
Hello Lua
2、从C++调用LUA,有两个参数,有一个返回值
int main(int argc, const char * argv[]) {
lua_State *lua_state;
lua_state = luaL_newstate();
static const luaL_Reg lualibs[] = {
{ "base", luaopen_base },
{ NULL , NULL}
};
const luaL_Reg *lib = lualibs;
for ( ; lib->func != NULL ; lib++) {
lib->func(lua_state);
lua_settop(lua_state, 0);
}
//*********************** C++ -> Lua **********************
//========================hello.lua=========================
luaL_dofile(lua_state, "hello.lua");
lua_getglobal(lua_state, "add");
lua_pushnumber(lua_state, 4);
lua_pushnumber(lua_state, 5);
lua_call(lua_state, 2, 1);
int rs1 = lua_tonumber(lua_state, -1);
printf("caculate the result = %d\n", rs1);
lua_close(lua_state);
return 0;
}
在hello.lua定义add 方法
function add(x,y)
return x+y
end
运行结果为:
caculate the result = 9
3、从LUA调C++定义的方法
<span style="color:#330000;"></span><p style="margin-top: 0px; margin-bottom: 0px; font-size: 14px; font-family: Menlo;"><span style="color:#330000;">#include "lua.hpp"</span></p>
static int average(lua_State *L)
{
int n = lua_gettop(L);
double sum = 0;
int i;
for (i = 1; i<= n; i++) {
if (!lua_isnumber(L, i)) {
lua_pushstring(L, "Incorrect argument to 'average'");
lua_error(L);
}
sum += lua_tonumber(L, i);
}
lua_pushnumber(L, sum/n);
lua_pushnumber(L, sum);
return 2;
}
static int caculate(lua_State *L)
{
// printf("the caculate result\n");
int n = lua_gettop(L);
// int result = 100000;
lua_pushnumber(L, n);
return 3;
}
int main(int argc, const char * argv[]) {
lua_State *lua_state;
lua_state = luaL_newstate();
static const luaL_Reg lualibs[] = {
{ "base", luaopen_base },
{ NULL , NULL}
};
const luaL_Reg *lib = lualibs;
for ( ; lib->func != NULL ; lib++) {
lib->func(lua_state);
lua_settop(lua_state, 0);
}
//*********************** Lua -> C++ **********************
lua_register(lua_state, "average", average);
lua_register(lua_state, "caculate", caculate);
luaL_dofile(lua_state, "hello.lua");
lua_getglobal(lua_state, "avg");
std::cout<<"avg is "<<lua_tointeger(lua_state, -1)<<std::endl;
lua_pop(lua_state, 1);
lua_getglobal(lua_state, "sum");
std::cout<<"sum is:"<<lua_tointeger(lua_state,-1)<<std::endl;
lua_close(lua_state);
return 0;
}
在hello.lua定义add 方法
avg,sum = average(1,2,3,4,5)
avg is 3
sum is:15