lua和c语言哪个好学,C程序与Lua脚本相互调用

Lua脚本是一种可用于C程序开发/测试的工具,本篇介绍一下C程序与Lua脚本如何进行相互调用,更加详细的操作参见《Programing in Lua》。本文分为3个部分:1、Windows环境下Lua的下载以及安装注意事项;2、Visual C++6.0中Lua的配置;3、C程序与Lua脚本相互调用实例。

1、Windows环境下Lua的下载以及安装注意事项

a、下载Lua for Windows,笔者用的版本是V5.1.4-35;

b、上微软官网,下载Visual C++运行库——vcredist_x86.exe;

c、将LuaForWindows_v5.1.4-35.exe和vcredist_x86.exe放在同一目录下,直接点击LuaForWindows_v5.1.4-35.exe,安装即可,建议将Lua安装在D盘根目录下;

d、安装成功之后,使用Lua目录下SciTE编辑器,就可以编写lua脚本,点击“执行”按钮,就可以查看执行结果。

JjCGbqjgeHiiRjT9.png

2、Visual C++6.0中Lua的配置

a、新建一个工程LuaMutualCallCMethod,选择Tools--->Options--->Directories,配置VC++的目录项:

(1)Include files,添加“D:\LUA\5.1\INCLUDE”;

(2)Library files,添加“D:\LUA\5.1\LIB”;

(3)Executable files,添加“D:\LUA\5.1”;

b、配置工程的链接属性,选择Project--->Setting---->Link,添加lua5.1.lib;

3、C程序与Lua脚本相互调用实例

a、C程序调用Lua脚本

(1)创建Lua数据脚本data.lua

7LeDHSe7b45W15nf.gif

xurk0RozD16dW41g.gif

length = 5

width = 10

heigth = 20

View Code

(2)创建C程序main.c,读取test.lua中的数据,并打印输出

edLtZbq94B7dtouX.gif

LMxKQzl5yqNyYUdB.gif

#include

#include

#include

#include

void main()

{

int retCode;

lua_State *L = luaL_newstate();

luaL_openlibs(L);

retCode = luaL_dofile(L,"data.lua");

if (retCode != 0)

{

printf("error %s\n",lua_tostring(L,-1));

return;

}

lua_getglobal(L,"length");

lua_getglobal(L,"width");

lua_getglobal(L,"heigth");

printf("length=%d\n",lua_tointeger(L,-3));

printf("width=%d\n",lua_tointeger(L,-2));

printf("heigth=%d\n",lua_tointeger(L,-1));

lua_close(L);

}

View Code

(3)输出结果

length = 5

width = 10

height = 20

b、Lua脚本调用C程序中的函数

(1)创建Lua执行脚本compute.lua

2cLFpbe6QaFaygaS.gif

V9m3ukduqYguZcY9.gif

sum = 0

firstPara = 20

secondPara = 10

sum = addMethod(firstPara,secondPara)

printMethod(sum)

View Code

(2)创建C程序CMethodForLua.c,编写与“addMethod”、“printMethod”相对应的C函数,并将其“注册”到Lua环境中;

0XCb84pLGw1jlXWi.gif

78jjdai9To4xx848.gif

#include

#include "CmethodForLua.h"

/**********************************************************************

Fuction: Lua_AddMethod

Description: 供Lua调用的加法运算

Parameter: luaEnv --[in] lua执行环境

Return Value: 数字 lua执行后返回参数

Note:

Other: 内部函数,仅供CmethodForLua.c调用

*********************************************************************/

static int Lua_AddMethod(lua_State *luaEnv)

{

double firstPara = luaL_checknumber(luaEnv,1);

double secondPara = luaL_checknumber(luaEnv,2);

lua_pushnumber(luaEnv,firstPara+secondPara);

return 1;

}

/**********************************************************************

Fuction: Lua_PrintMethod

Description: 供Lua调用的打印运算

Parameter: luaEnv --[in] lua执行环境

Return Value: 数字 lua执行后返回参数

Note:

Other: 内部函数,仅供CmethodForLua.c调用

*********************************************************************/

static int Lua_PrintMethod(lua_State *luaEnv)

{

int printData = (int)luaL_checknumber(luaEnv,1);

printf("The Print Data is %d\n",printData);

return 0;

}

/**********************************************************************

Fuction: OpenLuaExecuteEnvironment

Description: 打开Lua执行环境

Parameter: luaEnv --[in/out] lua执行环境

Return Value: 0 执行成功

非0 执行失败,错误码

Note:

1、创建Lua状态;

2、打开Lua标准库

3、注册供Lua调用的C函数;

Other:

*********************************************************************/

int OpenLuaExecuteEnvironment(lua_State **luaEnv)

{

lua_State *L = NULL;

L = luaL_newstate();

luaL_openlibs(L);

lua_register(L,"addMethod",Lua_AddMethod);

lua_register(L,"printMethod",Lua_PrintMethod);

*luaEnv = L;

return LUA_SUCCESS;

}

/**********************************************************************

Fuction: CloseLuaExecuteEnvironment

Description: 关闭Lua执行环境

Parameter: luaEnv --[in] lua执行环境

Return Value: 0 执行成功

非0 执行失败,错误码

Note:

Other:

*********************************************************************/

int CloseLuaExecuteEnvironment(lua_State *luaEnv)

{

lua_close(luaEnv);

return LUA_SUCCESS;

}

View Code

(3)创建main.c,获取CMethodForLua.c中的Lua执行环境,并执行compute.lua脚本

LHx0UCgmNRAXKsHQ.gif

2Cas6Iqy2EKdWshM.gif

#include

#include "CmethodForLua.h"

void main()

{

lua_State *luaEnv = NULL;

int retCode;

retCode = OpenLuaExecuteEnvironment(&luaEnv);

if (retCode != LUA_SUCCESS)

{

return;

}

//Lua调用C语言函数,

retCode = luaL_dofile(luaEnv,"compute.lua");

if (retCode != LUA_SUCCESS)

{

printf("error %s\n",lua_tostring(luaEnv,-1));

return;

}

CloseLuaExecuteEnvironment(luaEnv);

}

View Code

(4)输出结果

The Print Data is 30

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值