简答的lua调用c++函数例子
2012-04-19 21:51
781 查看
[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函数及C++和lua相互函数调用小例子
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- 简单C++调用lua函数及C++和lua相互函数调用小例子
- 如何在c/c++中调用LUA函数
- cocos2d-x下c++调用lua函数
- c/c++中调用lua第一个例子
- C++中调用Lua函数实例
- VC6.0配置LUA编译环境及LUA调用C++函数的简单示例
- lua入门之二:c/c++ 调用lua及多个函数返回值的获取
- C++中,嵌套类调用外部类函数例子
- Lua学习笔记(6) : 在Lua中调用C++之函数
- Lua调用C++的函数
- cocos2dx中注册导出c++函数供lua调用(使用tolua++)
- c++ 调用lua函数简单案例
- tolua++绑定C/C++语言函数供lua调用
- Lua中调用C++函数实例
- C/C++读取Lua中的变量及调用Lua函数
- C++ 调用Lua简单例子