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脚本,点击“执行”按钮,就可以查看执行结果。
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
length = 5width= 10heigth= 20
View Code
(2)创建C程序main.c,读取test.lua中的数据,并打印输出
#include #include#include#include
voidmain()
{intretCode;
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
sum = 0firstPara= 20secondPara= 10sum=addMethod(firstPara,secondPara)
printMethod(sum)
View Code
(2)创建C程序CMethodForLua.c,编写与“addMethod”、“printMethod”相对应的C函数,并将其“注册”到Lua环境中;
#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;returnLUA_SUCCESS;
}/**********************************************************************
Fuction: CloseLuaExecuteEnvironment
Description: 关闭Lua执行环境
Parameter: luaEnv --[in] lua执行环境
Return Value: 0 执行成功
非0 执行失败,错误码
Note:
Other:
*********************************************************************/
int CloseLuaExecuteEnvironment(lua_State *luaEnv)
{
lua_close(luaEnv);returnLUA_SUCCESS;
}
View Code
(3)创建main.c,获取CMethodForLua.c中的Lua执行环境,并执行compute.lua脚本
#include #include"CmethodForLua.h"
voidmain()
{
lua_State*luaEnv =NULL;intretCode;
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
Lua 的详细介绍:请点这里
Lua 的下载地址:请点这里