交叉编译ubus

一、环境准备

  1. Linux系统环境
$ uname -a
Linux localhost.localdomain 5.14.0-284.30.1.el9_2.x86_64 #1 SMP PREEMPT_DYNAMIC Fri Aug 25 09:13:12 EDT 2023 x86_64 x86_64 x86_64 GNU/Linux
  1. cmake版本
$ cmake --version
cmake version 3.20.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).
  1. pkg-config版本
$ pkg-config --version
1.7.3
  1. 交叉编译器
$ /opt/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gcc --version 
mipsel-openwrt-linux-gcc (OpenWrt GCC 7.5.0 r11427-9ce6aa9d8d) 7.5.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  1. 设置环境变量

本文演示的编译方法是在“全新”安装的Linux系统上进行的,请按需设置相关的环境变量。
PATH环境变量用来设置交叉编译器的搜索路径。STAGING_DIR环境变量用来设置交叉编译器(mipsel-openwrt-linux-gcc)编译过程中用到的头文件、库文件的搜索路径,其它交叉编译器可能并不需要该环境变量。

export PATH=$PATH:/opt/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin
export CC=mipsel-openwrt-linux-gcc
export STAGING_DIR=/opt/toolchain-mipsel_24kc_gcc-7.5.0_musl
  1. 下载源码
git clone https://github.com/json-c/json-c.git
git clone https://git.openwrt.org/project/libubox.git
git clone https://git.openwrt.org/project/ubus.git

json-c commit: ad8b8afa7d567053b87f2d37ee4a534e13c210c7
libubox commit: 75a3b870cace1171faf57bd55e5a9a2f1564f757
ubux commit: f787c97b34894a38b15599886cacbca01271684f

  1. 新建编译生成目录
$ mkdir ~/dependencies

二、编译json-c

1. 进入json-c源码目录,按序执行以下命令编译json-c

cmake . -DCMAKE_INSTALL_PREFIX=~/dependencies/json-c
make && make install

2. 编译完成后~/dependencies/json-c的目录结构

├── json-c
│   ├── include
│   │   └── json-c
│   │       ├── arraylist.h
│   │       ├── debug.h
│   │       ├── json_config.h
│   │       ├── json_c_version.h
│   │       ├── json.h
│   │       ├── json_inttypes.h
│   │       ├── json_object.h
│   │       ├── json_object_iterator.h
│   │       ├── json_patch.h
│   │       ├── json_pointer.h
│   │       ├── json_tokener.h
│   │       ├── json_types.h
│   │       ├── json_util.h
│   │       ├── json_visit.h
│   │       ├── linkhash.h
│   │       └── printbuf.h
│   └── lib
│       ├── cmake
│       │   └── json-c
│       │       ├── json-c-config.cmake
│       │       ├── json-c-targets.cmake
│       │       └── json-c-targets-debug.cmake
│       ├── libjson-c.a
│       ├── libjson-c.so -> libjson-c.so.5
│       ├── libjson-c.so.5 -> libjson-c.so.5.3.0
│       ├── libjson-c.so.5.3.0
│       └── pkgconfig
│           └── json-c.pc

三、编译libubox

1. 将json-c的pkgconfig文件添加到pkg-config脚本搜索路径

export PKG_CONFIG_PATH=~/dependencies/json-c/lib/pkgconfig

2. 设置cmake FIND_PACKAGE函数搜索路径

export CMAKE_PREFIX_PATH=~/dependencies/json-c/lib

3. 进入libubox目录,按序执行以下命令编译libubox

cmake . -DBUILD_LUA=OFF -DCMAKE_INSTALL_PREFIX=~/dependencies/libubox
make && make install

4. 编译完成后~/dependencies/libubox的目录结构

├── libubox
│   ├── bin
│   │   └── jshn
│   ├── include
│   │   └── libubox
│   │       ├── assert.h
│   │       ├── avl-cmp.h
│   │       ├── avl.h
│   │       ├── blob.h
│   │       ├── blobmsg.h
│   │       ├── blobmsg_json.h
│   │       ├── json_script.h
│   │       ├── kvlist.h
│   │       ├── list.h
│   │       ├── md5.h
│   │       ├── runqueue.h
│   │       ├── safe_list.h
│   │       ├── ulog.h
│   │       ├── uloop.h
│   │       ├── usock.h
│   │       ├── ustream.h
│   │       ├── utils.h
│   │       └── vlist.h
│   ├── lib
│   │   ├── libblobmsg_json.a
│   │   ├── libblobmsg_json.so
│   │   ├── libjson_script.so
│   │   ├── libubox.a
│   │   └── libubox.so
│   └── share
│       └── libubox
│           └── jshn.sh

四、编译ubus

1. 设置libubox库搜索路径

export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:~/dependencies/libubox/lib

2. 设置libubox头文件搜索路径

export CMAKE_INCLUDE_PATH=~/dependencies/libubox/include

3. 进入ubus目录,按序执行以下命令编译ubus

make && make install

4. 编译完成后/dependencies/ubus的目录结构

└── ubus
    ├── bin
    │   └── ubus
    ├── include
    │   ├── libubus.h
    │   ├── ubus_common.h
    │   └── ubusmsg.h
    ├── lib
    │   └── libubus.so
    └── sbin
        └── ubusd
posted @   _jtchen  阅读(201)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示