Openwrt 软件模块之 libubox

目录:

libubox 模块简要

libubox 库源码

libubox 编译

实用工具 jshn

libubox 模块简要

该模块是 Openwrt 的一个核心库,提供了一系列基础API,我们后续要介绍的 Ubus也会用到这个库,可见这个库是多么重要

提供的功能:

AVL 树,加解密,json,链表,网络socket, 事件驱动,任务管理等等

├── avl.c
├── avl-cmp.c
├── avl-cmp.h
├── avl.h
├── base64.c
├── blob.c
├── blob.h
├── blobmsg.c
├── blobmsg.h
├── blobmsg_json.c
├── blobmsg_json.h
├── CMakeFiles
│   ├── CMakeDirectoryInformation.cmake
│   ├── jshn.dir
│   │   ├── build.make
│   │   ├── C.includecache
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.internal
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   ├── progress.marks
│   ├── ubox.dir
│   │   ├── build.make
│   │   ├── C.includecache
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.internal
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   └── progress.make
│   └── ubox-static.dir
│       ├── build.make
│       ├── C.includecache
│       ├── cmake_clean.cmake
│       ├── cmake_clean_target.cmake
│       ├── DependInfo.cmake
│       ├── depend.internal
│       ├── depend.make
│       ├── flags.make
│       ├── link.txt
│       └── progress.make
├── cmake_install.cmake
├── CMakeLists.txt
├── format.c
├── format.h
├── jshn.c
├── json.c
├── json.h
├── jsonrpc.c
├── jsonrpc.h
├── json_script.c
├── json_script.h
├── kvlist.c
├── kvlist.h
├── list.h
├── lua
│   ├── CMakeFiles
│   │   ├── CMakeDirectoryInformation.cmake
│   │   └── progress.marks
│   ├── cmake_install.cmake
│   ├── CMakeLists.txt
│   ├── Makefile
│   └── uloop.c
├── Makefile
├── md5.c
├── md5.h
├── printbuf.c
├── printbuf.h
├── runqueue.c
├── runqueue.h
├── safe_list.c
├── safe_list.h
├── server.c
├── sh
│   └── jshn.sh
├── types.h
├── ulog.c
├── ulog.h
├── uloop.c
├── uloop.h
├── unformat.c
├── usock.c
├── usock.h
├── ustream.c
├── ustream-fd.c
├── ustream.h
├── utils.c
├── utils.h
├── vlist.c
└── vlist.h

libubox 库源码

git clone https://github.com/yubo/libubox.git

libubox 编译

需安装 cmake,生成 Makefile

 无需编译 lua 及 example

cmake -D BUILD_LUA:BOOL=OFF -D BUILD_EXAMPLES:BOLL=OFF .

 编译及安装

实用工具 jshn

当完成安装后,jshn 工具已经安装到系统

 可是运行缺提示加载 libubox.so 库 错误

排除库错误

1. 使用 ldd 查看库链接情况,可见的确没找到

 2. 查看系统配置,搜索路径下已经有该文件

3. 既然路径都是正常的为什么没有链接上呢?

    可能原因就是我们新安装的这个库还未被缓存到文件 /etc/ld.so.cache中

    所以我们需要手动刷新这个 cache(记得用sudo)

 4. 再次查看,链接成功

执行示例 1

尽管我们可以用 jshn 工具来读取,设定 JSON 字符串,但是并不便利

我们需要用到一个叫  jshn.sh 的脚本,模块的使用更加便捷,正如其内容描述

# functions for parsing and generating json

该文件并没有在源码中存有,只是提供了一个同名文件的示例(libubox/examples/jshn.sh)

json_init
json_add_string "msg" "Hello, world!"
json_add_object "test"
json_add_int "testdata" "1"
json_close_object
MSG=`json_dump`
# MSG now contains: { "msg": "Hello, world!", "test": { "testdata": 1 } }


# parsing json data
json_load "$MSG"
json_select test
json_get_var var1 testdata
json_select ..
json_get_var var2 msg
echo "msg: $var2 - testdata: $var1"

注意在使用 json_X 这些function时,需要 source 一下

 function 提供的功能也不用细说,观其名知其意

然后修改下这个示例,

再次运行:

执行示例 2

我们在实际运行的 Openwrt 环境来试试

小结:

正如文章开头所讲,作为一个核心库,我们在研习 openwrt 时会看见很多 API 的调用,其实用到的就是这里面的,如果想更清晰的把握,学有余力的情况可以看看这部分的代码

下一章我们会开始另一个重要部分的学习:Ubus

查看更多Openwrt笔记

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值