十 ubus安装编译
?
ubus是openwrt平臺上的一種進程間通信機制,用起來非常方便,且移植性強,符合設計中的迪米特原則,可以異步開發。因此被擴展到很多軟件中,通用性好,不用重造輪子,也方便與其它人共同開發。我們自己在Coding過程中,或設計系統框架時,也可以將ubus作為系統的一般機制來使用。
目錄
一、源碼包獲取
1. 源碼包可以通過下載openWRT中的源碼。
2. 或獲取ubus的git庫
3. 或通過下面分享鏈接 (也是從openwrt里單獨提取的,筆者測試過)
二、構建過程
三、簡單測試
一、源碼包獲取
1. 源碼包可以通過下載openWRT中的源碼。
http://downloads.openwrt.org.cn/sources/
2. 或獲取ubus的git庫
git clone git://nbd.name/luci2/ubus.git
git clone git://nbd.name/luci2/ubox.git
3. 或通過下面分享鏈接 (也是從openwrt里單獨提取的,筆者測試過)
git clone https://gitee.com/wangxinyu2011/ubus_build.git
后面的安裝測試,筆者以第3種方式中的版本為例。
二、構建過程
要想正確使用ubus,需要以下3個源碼包,構建順序及依賴關系如下:
json-c <--- libubox <--- ubus
注意:json、libubox、ubus, 這三者的版本需要有一定的匹配。
?
| 名稱 | 編譯方法 | 生成文件 | 重要頭文件 |
| json-c-0.12 json數據處理 | ./configure make make install | .lib/libjson-c.a .lib/libjson-c.so .lib/libjson-c.so.2 .lib/libjson-c.so.2.0.1 | json/json.h |
| libubox-2014-08-04 事件驅動模型機制實現,類似libev | 在CMakeLists.txt中做如下修改 1、OPTION(BUILD_LUA "build Lua plugin" OFF) 2、增加 include_directories("/usr/local/include") link_directories("/usr/local/lib") 執行cmake ./ 執行make / make install ? | libblobmsg_json.so libjson_script.so libubox.so | libubox/uloop.h libubox/ustream.h libubox/utils.h libubox/blobmsg_json.h |
| ubus-2014-09-17 ubus的實現 | 同上 | libubus.so ubus ubusd | libubus.h |
| example測試代碼 | 進入測試代碼目錄進行make | —— | —— |
?
說明 :
1、在編譯json-c-0.12后,共生成的動態庫在/usr/local/lib/目錄中,頭文件在/usr/local/include/json-c目錄中。
為了后續能夠正常鏈接到庫文件
echo "/usr/local/lib" >> /etc/ld.so.confldconfig命令1是為將鏈接庫路徑增加至系統目錄。命令2是更新鏈接庫。
?
在libubox會使用json-c的頭文件,但其使用時均為json/json.h ,因此,需要創建鏈接以便正確引用到頭文件。
ln -s /usr/local/include/json-c /usr/local/include/json?
2、cmake 命令不過多擴展。CMakeLists.txt 為編譯配置文件,編譯哪些文件,頭文件、庫路徑,以及編譯選項,生成文件,均在此進行配置。執行cmake . 后, 會生成CMakeCache.txt 、CMakeFiles以及Makefile文件,如果重新修改CMakeLists.txt文件后,需要刪除CMakeCache.txt 文件,以保證修改后的配置生效。
在此增加的include_directories、link_directories(可大寫)命令,就是依賴的頭文件、庫的路徑, 這個地址取決于install依賴文件的路徑。
?
3、ubus及ubusd安裝路徑在/usr/local/sbin目錄中,如果找不到,則需要手動添加到系統路徑中。
echo "export PATH=\$PATH:/usr/local/sbin" >> /etc/profilesource /etc/profile?
三、簡單測試
先拉起ubusd,然后進行一個簡單的事件監聽與發送的測試。
拉起ubusd , 注意測試過程中使用統一的權限,不建議使用root權限。 root@wangxinyu-PC:/home/wangxinyu/work/YD_8189D/AC1201_SVN2908# ubusd查看ubus命令 root@wangxinyu-PC:/home/wangxinyu/work/GITEE/ubus_build/ubus-2014-09-17# ubus Usage: ubus [<options>] <command> [arguments...] Options:-s <socket>: Set the unix domain socket to connect to-t <timeout>: Set the timeout (in seconds) for a command to complete-S: Use simplified output (for scripts)-v: More verbose outputCommands:- list [<path>] List objects- call <path> <method> [<message>] Call an object method- listen [<path>...] Listen for events- send <type> [<message>] Send an event- wait_for <object> [<object>...] Wait for multiple objects to appear on ubusroot@wangxinyu-PC:/home/wangxinyu/work/GITEE/ubus_build/ubus-2014-09-17# ubus list test_invoke root@wangxinyu-PC:/home/wangxinyu/work/GITEE/ubus_build/ubus-2014-09-17# root@wangxinyu-PC:/home/wangxinyu/work/GITEE/ubus_build/ubus-2014-09-17# 在一個終端中拉起事件監聽 root@wangxinyu-PC:/home/wangxinyu/work/GITEE/ubus_build/ubus-2014-09-17# ubus listen test{ "test": {"msg":"Hello world."} }在另一個終端中發送事件 root@wangxinyu-PC:/home/wangxinyu/work/ubus_test/example/01_invoke# ubus send test '{"msg":"Hello world."}' root@wangxinyu-PC:/home/wangxinyu/work/ubus_test/example/01_invoke# root@wangxinyu-PC:/home/wangxinyu/work/ubus_test/example/01_invoke# root@wangxinyu-PC:/home/wangxinyu/work/ubus_test/example/01_invoke#?
關于ubus使用測試、原理見后續文檔。
總結
以上是生活随笔為你收集整理的十 ubus安装编译的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 九 Deepin配置ssh访问gitee
- 下一篇: 2.整合管理