C++中使用Lua脚本 和lua中调用c的方法

参考http://blog.csdn.net/kun1234567/archive/2007/12/11/1929815.aspx
第1步:下载
从官方主页www.lua.org下载Lua源代码,最新版本为5.1.3。
解压之后找到“[Lua]/src”文件夹,这里面就是Lua了,不过还不能直接使用。

第2步:编译lua
使用任意ANSI C编译器,在这里使用VS2005编译LUA。具体步骤如下:
a.打开vs的命令行工具,工具-->visual studio 2005 command prompt
b.跳转到[Lua]目录,例如:cd D:/Program Files/Lua
c.执行:etc\luavs.bat( 注意,是 \ 不是 /,写错了不能执行编译 )
d.然后lua51.dll, lua51.lib, lua.exe, and luac.exe就生成在 src 路径下了
e.在windows环境变量中把[Lua]\src添加到系统Path中去,

第3步:创建lua脚本
下载LuaEdit http://luaforge.net/frs/download.php/1037/LuaEdit_2_5.zip 他是Lua脚本的编辑工具,还可以对Lua脚本进行语法检测和调试。你也可以其他的纯文本编辑工具写一个文件 test.lua,注意后面是没有分号‘;’的
function f ( x, y)
return x + y
end

第4步:在C++ 中调用Lua脚本
开启VC++6.0环境创建一个新文件main.cpp
// Win32Console.cpp : Defines the entry point for the console application.
//

#include
" stdafx.h "

extern " C "
{
#include
"D:/Lib/Lua/lua.h"
#include
"D:/Lib/Lua/lualib.h"
#include
"D:/Lib/Lua/lauxlib.h"
}

#pragma comment( lib ,
" D:/Lib/Lua/lua51.lib " )
lua_State
* L;

// 调用lua
double fun( double x, double y )
{
double ret;
lua_getglobal( L,
"add"); // 获取全局变量f
lua_pushnumber( L,x); // 操作数压栈
lua_pushnumber( L,y); // 操作数压栈
lua_call( L, 2, 1); // 执行:2个操作数,1个返回值
//lua_pcall( L, 2, 1, 0); // 保护模式的lua_call,0为错误处理码。具体应用暂时不明,在使用手册中有粗略介绍
ret = lua_tonumber( L, -1); // 将栈顶元素转换成数字并赋值给ret
lua_pop( L, 1); // 从栈中弹出一个元素
return ret;
}


// 被lua调用的方法
static int average(lua_State * L2)
{
/**//* get number of arguments */
int n = lua_gettop(L2);
double sum = 0;
int i;
/**//* loop through each argument */
for (i = 1; i <= n; i++)
{
/**//* total the arguments */
sum
+= lua_tonumber(L2, i);
}

lua_pushnumber(L, sum
/ n);
/**//* push the sum */
lua_pushnumber(L, sum);
/**//* return the number of results */
printf(
"average called. [ok]\n");
return 2;
}

// ==============================================
// Main Functions
// ==============================================
int main( void )
{
int error;
L
= lua_open(); // 创建Lua接口指针(借用DX的术语,本质是个堆栈指针)
luaopen_base(L); // 加载Lua基本库
luaL_openlibs(L); // 加载Lua通用扩展库
/**//*
可能有的文章会采用以下写法,手工控制加载哪些库:
luaopen_table(L); // 加载table库
luaopen_io(L); // 加载IO库
luaopen_string(L); // 加载string库
luaopen_math(L); // 加载math库
经过测试,luaopen_io(L);该句执行异常,可能跟Lua的IO库有关系。具体原因暂时没有追究,将来如果有机会弄清楚,再回头来阐述。
*/


/**//* load the script */
lua_register(L,
"average", average);
error
= luaL_dofile(L, "hellow.lua"); // 读取Lua源文件到内存编译

double ret = fun( 10, 3.4); // 调用模版函数f
printf( "ret = %f\n", ret); // 输出结果,C语言的东西,跟Lua无关
lua_close( L);
return 1;
}


创建一个hellow.lua文件和main.cpp放在一起,写入以下内容
function add ( x, y)
file
= assert(io.open( " data.txt " , " w " ))
file:write(
" abcde\n " )
file:write(
" ok!\n " )
file:close()

-- DataDumper( 1 , 2 , 3 , 4 )
file
= assert(io.open( " data.txt " , " r " ))
str
= file:read( " *a " )
io.write(str)
io.write(
" \n " )

avg, sum
= average( 10 , 200 , 3000 )
print(
" The average is " , avg)
print(
" The sum is " , sum)

return x + y
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值