Lua脚本是一种可用于C程序开发/测试的工具,本篇介绍一下C程序与Lua脚本如何进行相互调用,更加详细的操作参见《Programing in Lua》。本文分为3个部分:1、Windows环境下Lua的下载以及安装注意事项;2、Visual C++6.0中Lua的配置;3、C程序与Lua脚本相互调用实例。Lua程序设计 Programming in Lua 中文 PDF 高清版 下载:http://www.linuxidc.com/Linux/2015-05/117362.htm 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:LUA5.1INCLUDE”; (2)Library files,添加“D:LUA5.1LIB”; (3)Executable files,添加“D:LUA5.1”; b、配置工程的链接属性,选择Project--->Setting---->Link,添加lua5.1.lib; 3、C程序与Lua脚本相互调用实例 a、C程序调用Lua脚本 (1)创建Lua数据脚本data.lua length = 5width = 10heigth = 20View Code (2)创建C程序main.c,读取test.lua中的数据,并打印输出 #include <stdio.h>#include <lua.h>#include <lauxlib.h>#include <lualib.h>void main(){int retCode;lua_State *L = luaL_newstate();luaL_openlibs(L);retCode = luaL_dofile(L,"data.lua");if (retCode != 0){printf("error %s
",lua_tostring(L,-1));return;}lua_getglobal(L,"length");lua_getglobal(L,"width");lua_getglobal(L,"heigth");printf("length=%d
",lua_tointeger(L,-3));printf("width=%d
",lua_tointeger(L,-2));printf("heigth=%d
",lua_tointeger(L,-1));lua_close(L);}View Code (3)输出结果 length = 5 width = 10 height = 20 b、Lua脚本调用C程序中的函数 (1)创建Lua执行脚本compute.luasum = 0firstPara = 20secondPara = 10sum = addMethod(firstPara,secondPara)printMethod(sum)View Code (2)创建C程序CMethodForLua.c,编写与“addMethod”、“printMethod”相对应的C函数,并将其“注册”到Lua环境中;#include <stdio.h>#include "CmethodForLua.h"/**********************************************************************Fuction:Lua_AddMethodDescription:供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_PrintMethodDescription:供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
",printData);return 0;}/**********************************************************************Fuction:OpenLuaExecuteEnvironmentDescription:打开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:CloseLuaExecuteEnvironmentDescription:关闭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脚本#include <stdio.h>
#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
",lua_tostring(luaEnv,-1));
return;
}
CloseLuaExecuteEnvironment(luaEnv);
}View Code (4)输出结果 The Print Data is 30Lua 语言 15 分钟快速入门 http://www.linuxidc.com/Linux/2013-06/86582.htmLua程序设计(第2版)中文 PDF http://www.linuxidc.com/Linux/2013-03/81833.htmLua程序设计(第二版)阅读笔记 http://www.linuxidc.com/Linux/2013-03/81834.htmNetBSD 将支持用 Lua 脚本开发内核组件 http://www.linuxidc.com/Linux/2013-02/79527.htmCentOS 编译安装 Lua LuaSocket http://www.linuxidc.com/Linux/2011-08/41105.htmLua 的详细介绍:请点这里
Lua 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-05/117361.htm