01:编译lua及C调用

我们今天在windows平台编译lua,生成 lua动态库,lua.exe,luac.exe

我把这个目录上传到giee,使用下面命令获取它:

git clone git@gitee.com:jameschenbo/lua_c_application.git

或者直接访问:访问网页

目录结构如下:

build.cmd 是编译脚本,在执行前我们需要修改里面的gcc路径

我使用的是本地安装好的QT5的编译器

点击运行 build.cmd 等待编译结束,生成lua文件夹

lua/bin/lua.exe 解析器

lua/bin/luac.exe 编译器

lua/bin/lua54.dll 动态库

lua/doc 帮助文档

lua/include 在其他平台使用lua需要包含的头文件

运行示例

进入example目录

运行build.cmd  同样需要更改里面GCC的路径

生成main.exe,执行如下:

c_call_main.c

#include "lua/include/lua.h"
#include "lua/include/lualib.h"
#include "lua/include/lauxlib.h"

static int clua_add(lua_State* L, int a, int b)
{
	int sum = 0;

	/* 函数入栈 */
	lua_getglobal(L, "add");

	/* 第一个函数参数入栈 */
	lua_pushnumber(L, a);

	/* 第二个函数参数入栈 */
	lua_pushnumber(L, b);

	/* 执行函数调用。2表示有两个函数形参,1表示add函数只有一个返回值,调用lua_call函数后lua自动出栈参数和函数,并将函数的执行结果入栈 */

	/*
	 * 执行函数调用
	 * 2表示lua脚本中add函数需要输入两个函数参数
	 * 1表示lua脚本中add函数有一个返回值
	 * 执行完函数调用后,lua自动出栈函数和参数
	 */
	lua_call(L, 2, 1);

	/*
	 * 得到函数add函数执行结果
	 * -1表示最后一个返回值,因为lua的函数可以返回多个值的。
	 */
	sum = lua_tonumber(L, -1);

	/* 出栈一个数据。此时栈中存的是add函数的执行结果,所以需要出栈 */
	lua_pop(L, 1);

	return sum;
}
/**
 * 调用lua 函数,传递参数并获取返回值
 * lua_script/add.lua
 */
void example_add(void)
{
	int sum = 0;

	lua_State* L;

	L = luaL_newstate();  /* 创建一个句柄 */

	luaL_openlibs(L);     /* 打开lua库 */

#if 1
	if(luaL_dofile(L, "./lua_script/add.lua"))  /* 从lua脚本文件 中加载lua脚本语句 */
	{
		printf(" load lua script file error! \r\n");
		return;
	}
#else
	if(luaL_dostring(L, (const char *)"function add(a, b) return a + b end"))  /* 从字符串中加载lua脚本语句 */
	{
		printf(" LUA语句有误!\r\n");
		return -1;
	}
#endif

	sum = clua_add(L, 10, 20);
	printf(" sum = %d \r\n", sum);


	lua_close(L);  /* 关闭lua,清理内存 */
}

void load_config_file(lua_State* L, const char* fname, int *w, int *h)
{
	if(luaL_loadfile(L, fname) || lua_pcall(L, 0, 0,0)) {
		printf("load config file error\n");
	}

	//1.读变量配置

	//入栈操作,和出栈操作要对应
	lua_getglobal(L, "width");
	lua_getglobal(L, "height");

    //出栈操作,先压栈的,后出栈
	if(!lua_isnumber(L, -2)) {
		printf("width should be number!\n");
	}

	if(!lua_isnumber(L, -1)) {
		printf("height should be number!\n");
	}
	//转换数据类型
	*w = lua_tointeger(L, -2);
	*h = lua_tointeger(L, -1);

	//清空栈
	lua_settop(L, 0);

	//2.读 table 配置
	lua_getglobal(L, "sys_table_cfg");
	//入栈,指定位置
	lua_getfield(L, -1, "sex");
	lua_getfield(L, -2, "age");
	lua_getfield(L, -3, "port");
	lua_getfield(L, -4, "baud");
	lua_getfield(L, -5, "isSave");

    //出栈,和入栈顺序相反
    printf("sys_table_cfg:\n\n");
	printf("sex:%s\n",lua_tostring(L, -5));
	printf("age:%d\n",lua_tointeger(L, -4));
	printf("port:%s\n",lua_tostring(L, -3));
	printf("baud:%d\n",lua_tointeger(L, -2));
	printf("isSave:%d\n",lua_tointeger(L, -1));
	printf("\n");
	lua_settop(L, 0);
}
/**
 * 读取lua格式的配置文件
 * lua_script/config.lua
 * 配置文件有全局变量,有表
 */
void example_config_file(void)
{
	lua_State *L = luaL_newstate();
	luaL_openlibs(L);

	int w, h;
	load_config_file(L, "./lua_script/config.lua", &w, &h);
	printf("width=%d,height=%d\n", w, h);
}
int main(int argc, char* argv[])
{
  //下面是两个例子选择一个编译执行
  //
	// example_add();  //调用lua函数例子,传参和接收返回值
	example_config_file();//读取lua脚本编写的配置文件例子

	getchar();
	return 0;
}		

lua当配置文件使用的脚本 config.lua

--define windows size
print "my application config file (*.lua)!"

width = 100

height = 80

sys_table_cfg = {
	sex = "male",
	age = 18,
	port = "COM1",
	baud = 9600,
	isSave = 0
}

lua函数调用脚本 add.lua

function add(a, b) 
	return a + b + 10
end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懂IT的嵌入式工程师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值