OpenWrt上UCI库的环境搭建 原创
1 简介
如果需要在 ubuntu 上直接使用 uci,那么参考这篇文章 — ubuntu 上安装 UCI:https://blog.csdn.net/rainforest_c/article/details/70142987
我们要在 ubuntu 上面编译 Openwrt 上运行的 C 程序,并且此程序需要调用 UCI 的 API,即此程序需要使用 libuci。
步骤:
- 安装libubox
- 安装libuci
- 编译应用程序
2 安装 libubox
libubox 是 libuci 依赖的动态库,故首先安装 libubox,步骤如下:
-
安装cmake
sudo apt-get install cmake
-
下载 libubox 源码及编译
创建编译目录并进入到编译目录:
root@hess-VirtualBox:/# mkdir /home/hess/uci_compile root@hess-VirtualBox:/# cd /home/hess/uci_compile
下载libubox源码并进入到源码目录:
root@hess-VirtualBox:/# git clone http://git.nbd.name/luci2/libubox.git libubox root@hess-VirtualBox:/# cd libubox
交叉编译源码:
root@hess-VirtualBox:/# export CC=arm-linux-gcc //使用交叉编译器 root@hess-VirtualBox:/# cmake CMakeList.txt root@hess-VirtualBox:/# cmake -DBUILD_LUA=off root@hess-VirtualBox:/# make root@hess-VirtualBox:/# make install //会自动安装到/usr/local
3、在 libubox 目录下的 libubox.so 文件即为所需动态库
3 安装 libuci
libuci 库是应用程序和 UCI 所需的动态库,安装步骤如下:
-
下载libuci源码及编译
下载libuci源码并进入到源码目录
root@hess-VirtualBox:/# git clone https://git.openwrt.org/project/uci.git libuci root@hess-VirtualBox:/# cd libuci
交叉编译源码
root@hess-VirtualBox:/# export CC=arm-linux-gcc //使用交叉编译器 root@hess-VirtualBox:/# cmake CMakeList.txt root@hess-VirtualBox:/# cmake -DBUILD_LUA=off root@hess-VirtualBox:/# make root@hess-VirtualBox:/# make install //会自动安装到/usr/local
-
在 libuci 目录下的 libuci.so 文件即为所需动态库
4 编译应用程序
-
创建源文件 uci_test.c,内容如下:
#include <stdio.h> #include <string.h> #include <uci.h> #include <stdlib.h> int main (int argc, char **argv) { struct uci_context *c; struct uci_ptr p; char *a = strdup ("wireless.@wifi-iface[0].ssid"); c = uci_alloc_context (); if (uci_lookup_ptr (c, &p, a, true) != UCI_OK) { uci_perror (c, "XXX"); return 1; } printf("%s\n", p.o->v.string); uci_free_context (c); free (a); return 0; }
-
编译
/* 通过 -I 和 -L 指定libuci的头文件和库 */
root@hess-VirtualBox:/# arm-linux-gcc -o uci_test uci_test.c -I/usr/local/include -L/usr/local/lib -luci
-
放入到 Openwrt 系统内运行,运行结果如下:
5 参考
https://oldwiki.archive.openwrt.org/doc/techref/uci https://blog.csdn.net/u013625451/article/details/83057765