Lua调用c++例子
2012-06-28 12:29
316 查看
[huangxw3@ubuntu]$cat main.lua
-- call a C++ function
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
=============================
[huangxw3@ubuntu]$cat test.cpp
#include <iostream>
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
using namespace std ;
/* the Lua interpreter */
lua_State* L;
static int average(lua_State *L)
{
/* get number of arguments */
int n = lua_gettop(L);
double sum = 0;
int i;
/* loop through each argument */
for (i = 1; i <= n; i++)
{
/* total the arguments */
if (!lua_isnumber(L, i))
{
lua_pushstring(L,"Incorrect argument to 'average'");
lua_error(L);
}
sum += lua_tonumber(L, i);
}
/* push the average */
lua_pushnumber(L, sum / n);
/* push the sum */
lua_pushnumber(L, sum);
/* return the number of results */
return 2;
}
int main ( int argc, char *argv[] )
{
int error ;
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaopen_base(L); //
加载Lua基本库
luaL_openlibs(L);
/* register our function */
lua_register(L, "average", average);
/* run the script */
error = luaL_dofile(L,"main.lua");
/* cleanup Lua */
lua_close(L);
return 0;
}
[huangxw3@ubuntu]$ g++ test.cpp -Wall -g-fPIC -lm -ldl -llua -D USER_DEF -o test
[huangxw3@ubuntu]$./test
The average is 30
The sum is 150
-- call a C++ function
avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)
=============================
[huangxw3@ubuntu]$cat test.cpp
#include <iostream>
#include <stdio.h>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
using namespace std ;
/* the Lua interpreter */
lua_State* L;
static int average(lua_State *L)
{
/* get number of arguments */
int n = lua_gettop(L);
double sum = 0;
int i;
/* loop through each argument */
for (i = 1; i <= n; i++)
{
/* total the arguments */
if (!lua_isnumber(L, i))
{
lua_pushstring(L,"Incorrect argument to 'average'");
lua_error(L);
}
sum += lua_tonumber(L, i);
}
/* push the average */
lua_pushnumber(L, sum / n);
/* push the sum */
lua_pushnumber(L, sum);
/* return the number of results */
return 2;
}
int main ( int argc, char *argv[] )
{
int error ;
/* initialize Lua */
L = lua_open();
/* load Lua base libraries */
luaopen_base(L); //
加载Lua基本库
luaL_openlibs(L);
/* register our function */
lua_register(L, "average", average);
/* run the script */
error = luaL_dofile(L,"main.lua");
/* cleanup Lua */
lua_close(L);
return 0;
}
[huangxw3@ubuntu]$ g++ test.cpp -Wall -g-fPIC -lm -ldl -llua -D USER_DEF -o test
[huangxw3@ubuntu]$./test
The average is 30
The sum is 150
相关文章推荐
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- C++调用lua例子
- C++调用Lua读取配置文件的一个简单例子
- 简答的lua调用c++函数例子
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- C++ 调用Lua简单例子
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- C++ 调用Lua简单例子
- C++ 调用Lua简单例子
- C++调用lua简单的例子
- c/c++中调用lua第一个例子
- LUA与C++互相调用简单例子
- lua环境的配置以及c++调用lua的简单例子
- 简单C++调用lua函数的小例子
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- C++ 调用Lua简单例子
- c++调用lua时简单打印lua栈
- Quick-Cocos2d-x 2.26 使用tolua工具导出C++的类给Lua调用WINDOWS篇
- cocos2dx关于在c/c++中调用lua函数
- C/C++调用lua脚本函数