临安做网站的公司有哪些,网站开发可选择的方案有,上海网站 备案,怎么查网站有没有做底部导航我们今天在windows平台编译lua#xff0c;生成 lua动态库,lua.exe#xff0c;luac.exe
我把这个目录上传到giee#xff0c;使用下面命令获取它:
git clone gitgitee.com:jameschenbo/lua_c_application.git
或者直接访问:访问网页
目录结构如下#xff1a; build.cmd 是…我们今天在windows平台编译lua生成 lua动态库,lua.exeluac.exe
我把这个目录上传到giee使用下面命令获取它:
git clone gitgitee.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.hstatic 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 1if(luaL_dofile(L, ./lua_script/add.lua)) /* 从lua脚本文件 中加载lua脚本语句 */{printf( load lua script file error! \r\n);return;}
#elseif(luaL_dostring(L, (const char *)function add(a, b) return a b end)) /* 从字符串中加载lua脚本语句 */{printf( LUA语句有误\r\n);return -1;}
#endifsum 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 100height 80sys_table_cfg {sex male,age 18,port COM1,baud 9600,isSave 0
}lua函数调用脚本 add.lua
function add(a, b) return a b 10
end