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,步骤如下:

  1. 安装cmake

    sudo apt-get install cmake
    
  2. 下载 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 所需的动态库,安装步骤如下:

  1. 下载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
    
  2. 在 libuci 目录下的 libuci.so 文件即为所需动态库

4 编译应用程序

  1. 创建源文件 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;
    }
    
  2. 编译

    /* 通过 -I 和 -L 指定libuci的头文件和库 */

    root@hess-VirtualBox:/# arm-linux-gcc -o uci_test uci_test.c -I/usr/local/include -L/usr/local/lib -luci
    
  3. 放入到 Openwrt 系统内运行,运行结果如下:

在这里插入图片描述

5 参考

https://oldwiki.archive.openwrt.org/doc/techref/uci
https://blog.csdn.net/u013625451/article/details/83057765

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenWRT是一个轻量级的嵌入式Linux发行版,适用于诸如路由器、防火墙等设备。LuCI(Lightweight User Interface Configuration)是一个为OpenWRT构建的Web管理界面框架。如果你想在本地开发OpenWRT的LuCI代码,你需要设置一个基本的Git开发环境。以下是搭建步骤: 1. **安装Git**: 确保你的系统已经安装了Git。如果你使用的是Linux或macOS,可以使用包管理器(如apt-get或brew)安装。 ```bash sudo apt-get install git # 对于Debian/Ubuntu brew install git # 对于macOS ``` 2. **克隆OpenWRT**: 在你的开发目录下,使用以下命令克隆OpenWRT的主仓和LuCI分支: ```bash git clone https://github.com/openwrt/openwrt.git cd openwrt git checkout -b luci-branch origin/luci ``` 3. **配置编译工具链**: 如果你需要自定义编译环境,确保安装交叉编译工具链,这通常根据你的目标硬件平台不同而异。OpenWRT有一个详细的文档指导:https://openwrt.org/docs/en/devel/developers_manual/compiling#cross-compilation 4. **获取LuCI源码**: 如果你想要单独管理LuCI的开发,可以在LuCI的GitHub上克隆: ```bash git clone https://github.com/LuaDist/luci.git luci ``` 5. **设置环境变量**: 配置你的系统路径,使得Git可以找到LuCI的源码: ```bash export PATH=$PATH:/path/to/luci ``` 6. **初始化项目**: 在每个仓中执行必要的初始化步骤,比如`cd luci && make` 或 `cd openwrt && make package_luci-core` 7. **提交和同步更改**: 完成开发后,记得定期将更改提交到本地仓,并将更改同步到远程仓: ```bash git add . git commit -m "Your commit message" git push ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半砖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值