上一篇介绍了简单的用C调用lua脚本,本文开始介绍如何用C调用lua并获取lua的多个返回值以及简单的JSON文件处理。
首先我们还是写一个lua脚本,姑且命名为function.lua
-- author: mleaf
-- mail: mleaf90@gmail.com
require("common")
local cjson = require "cjson"
local ROOT_PATH = "/tmp/mleaf.org/"
local JSON_NAME = "test.json"
function get_json_list()
local t = {}
local file_path = ROOT_PATH .. JSON_NAME
local ret,json = get_json_from_file(file_path)
if ret == 0 then
local json_list = json["json_list"]
for key,value in pairs(json_list) do
print(key, value["name"])
table.insert(t, value["name"]);
end
end
if next(t) == nil then --判断table是否为空
print("lua print:[name table is nil]")
ret = -4
end
return ret, t;
end
继续写function.lua所依赖的common.lua文件:
-- author: mleaf
-- mail: mleaf90@gmail.com
local cjson = require "cjson"
function table_is_empty(t)
return next(t) == nil
end
--[[
Read json file
]]
function get_json_from_file(file_name)
local ret,jsonData = 0,{}
-- 1:open file
local f = io.open(file_name, "r" )
if f == nil then
ret = -1
print("lua print:[file does not exist!]" .. ret)
return ret,jsonData
end
-- 2:read file
local t = f:read( "*all" )
f:close()
if t ~= nil and t ~= "" then
jsonData = cjson.decode(t);
if jsonData == nil or table_is_empty(jsonData) then
ret = -2
print("lua print:[Json parsing failure! str:]" .. t)
return ret, jsonData
end
else
ret = -3
print("lua print:[The file is empty!]" .. ret)
end
return ret,jsonData
end
然后当然是构造一个我们需要读取的json文件啦!
{
"json_list": [
{
"name": "mleaf"
}
]
}好了要读取的文件和lua文件都已经写完,我们开始写C程序。
#include <stdio.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
//调用lua中的get_json_list函数
void call_get_json_list(lua_State *L)
{
int i;
char *name;
lua_getglobal(L, "get_json_list"); //获取get_json_list函数
lua_pcall(L, 0, 2, 0); //调用函数,0个参数,2个返回值
int ret = (int)lua_tonumber(L, -2); //获取Lua的第一个返回值
printf("ret = %d\n", ret);
if(0 == ret){
int size = lua_objlen(L,-1);//相当于#table
for(i = 1; i <= size; i++)
{
lua_pushnumber(L, i);
lua_gettable(L, -2);
name = (char *)lua_tostring(L, -1); //获取栈顶元素(结果)
printf("name = %s\n", name);
//这时table[i]的值在栈顶了
lua_pop(L, 1);//把栈顶的值移出栈,保证栈顶是table以便遍历。
};
}
}
int main()
{
lua_State *L = luaL_newstate(); //新建lua解释器
luaL_openlibs(L); //载入lua所有函数库
luaL_dofile(L, "function.lua"); //执行"Test.lua"文件中的代码
call_get_json_list(L);
lua_close(L); //释放
return 0;
}PS:由于我们写的lua文件中数据读取以及处理成功获得我们想要的数据时返回0所以我们C程序以此判断数据读取正确。
接下来到了我们关键的一步了,你要问我是什么???
当然是编译啦,不编译怎么运行?
这次我们交叉编译,直接在openwrt上运行。
执行如下命令:
sudo /home/mleaf/work/lede/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16/bin/mipsel-openwrt-linux-gcc -o call-lua2 call-lua2.c -I/home/mleaf/work/lede/staging_dir/target-mipsel_24kc_musl-1.1.16/usr/include/ -L/home/mleaf/work/lede/staging_dir/target-mipsel_24kc_musl-1.1.16/usr/lib -llua -lm -ldl
当然你得改成你工具链所在的目录。
编译完成我们用winscp将所有文件拷贝到openwrt的/tmp/mleaf.org目录下
最后执行我们的C程序,记得添加权限chmod a+x call-lua2
我们修改一下test.json文件内容,删掉arry中的数据,变成如下内容:
{
"json_list": []
}
再次执行C程序:

最后啰嗦两句:楼主将程序直接集成到系统中后发现function.lua找不到common.lua文件,看了打印信息,openwrt默认会在“/usr/lib/lua”以及“/usr/share/lua”等文件中查找依赖文件,只需要将common.lua文件安装至以上目录即可,还有记得openwrt中需要装上lua-cjson
<*> lua-cjson……………………………………….. Lua CJSON parse
本站的文章和资源来自互联网或者站长的原创,按照 CC BY -NC -SA 3.0 CN协议发布和共享,转载或引用本站文章应遵循相同协议。如果有侵犯版权的资 源请尽快联系站长,我们会在24h内删除有争议的资源。欢迎大家多多交流,期待共同学习进步。







