一、参考资料
二、准备工作
1. 编译环境
- 宿主机:Ubuntu 20.04.6 LTS
- Host:ARM32位
- 交叉编译器:
arm-linux-gnueabihf-gcc-11.1.0
2. 设置交叉编译工具链
在交叉编译之前,需要设置交叉编译工具链的环境变量。
export PATH=/path/to/toolchains/arm-linux-gnueabihf/bin:$PATH
三、交叉编译cJSON
1. 下载源码
GitHub - DaveGamble/cJSON: Ultralightweight JSON parser in ANSI C
下载并解压源码。
tar -xvzf cJSON-1.7.18.tar.gz
2. 编译安装
cd cJSON-1.7.18
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/path/to/cjson/arm_install \
-DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
-DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
-DBUILD_SHARED_LIBS=ON \
-DENABLE_CJSON_TEST=OFF .. # 关闭测试程序
make -j8
make install
编译安装之后的文件目录:
yoyo@yoyo:~/360Downloads/cJSON-1.7.18$ tree -L 3 arm_install/
arm_install/
├── include
│ └── cjson
│ └── cJSON.h
└── lib
├── cmake
│ └── cJSON
├── libcjson.so -> libcjson.so.1
├── libcjson.so.1 -> libcjson.so.1.7.18
├── libcjson.so.1.7.18
└── pkgconfig
└── libcjson.pc
cmake编译:
yoyo@yoyo:~/360Downloads/cJSON-1.7.18/build$ cmake -DCMAKE_INSTALL_PREFIX=/path/to/cJSON-1.7.18/arm_install \
> -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc \
> -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ \
> -DBUILD_SHARED_LIBS=ON \
> -DENABLE_CJSON_TEST=OFF ..
CMake Deprecation Warning at CMakeLists.txt:2 (cmake_minimum_required):
Compatibility with CMake < 3.10 will be removed from a future version of
CMake.
Update the VERSION argument <min> value. Or, use the <min>...<max> syntax
to tell CMake that the project requires at least <min> but has been updated
to work with policies introduced by <max> or earlier.
-- The C compiler identification is GNU 11.1.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /path/to/toolchains/arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Performing Test FLAG_SUPPORTED_stdc89
-- Performing Test FLAG_SUPPORTED_stdc89 - Success
-- Performing Test FLAG_SUPPORTED_pedantic
-- Performing Test FLAG_SUPPORTED_pedantic - Success
-- Performing Test FLAG_SUPPORTED_Wall
-- Performing Test FLAG_SUPPORTED_Wall - Success
-- Performing Test FLAG_SUPPORTED_Wextra
-- Performing Test FLAG_SUPPORTED_Wextra - Success
-- Performing Test FLAG_SUPPORTED_Werror
-- Performing Test FLAG_SUPPORTED_Werror - Success
-- Performing Test FLAG_SUPPORTED_Wstrictprototypes
-- Performing Test FLAG_SUPPORTED_Wstrictprototypes - Success
-- Performing Test FLAG_SUPPORTED_Wwritestrings
-- Performing Test FLAG_SUPPORTED_Wwritestrings - Success
-- Performing Test FLAG_SUPPORTED_Wshadow
-- Performing Test FLAG_SUPPORTED_Wshadow - Success
-- Performing Test FLAG_SUPPORTED_Winitself
-- Performing Test FLAG_SUPPORTED_Winitself - Success
-- Performing Test FLAG_SUPPORTED_Wcastalign
-- Performing Test FLAG_SUPPORTED_Wcastalign - Success
-- Performing Test FLAG_SUPPORTED_Wformat2
-- Performing Test FLAG_SUPPORTED_Wformat2 - Success
-- Performing Test FLAG_SUPPORTED_Wmissingprototypes
-- Performing Test FLAG_SUPPORTED_Wmissingprototypes - Success
-- Performing Test FLAG_SUPPORTED_Wstrictoverflow2
-- Performing Test FLAG_SUPPORTED_Wstrictoverflow2 - Success
-- Performing Test FLAG_SUPPORTED_Wcastqual
-- Performing Test FLAG_SUPPORTED_Wcastqual - Success
-- Performing Test FLAG_SUPPORTED_Wundef
-- Performing Test FLAG_SUPPORTED_Wundef - Success
-- Performing Test FLAG_SUPPORTED_Wswitchdefault
-- Performing Test FLAG_SUPPORTED_Wswitchdefault - Success
-- Performing Test FLAG_SUPPORTED_Wconversion
-- Performing Test FLAG_SUPPORTED_Wconversion - Success
-- Performing Test FLAG_SUPPORTED_Wccompat
-- Performing Test FLAG_SUPPORTED_Wccompat - Success
-- Performing Test FLAG_SUPPORTED_fstackprotectorstrong
-- Performing Test FLAG_SUPPORTED_fstackprotectorstrong - Success
-- Performing Test FLAG_SUPPORTED_Wcomma
-- Performing Test FLAG_SUPPORTED_Wcomma - Failed
-- Performing Test FLAG_SUPPORTED_Wdoublepromotion
-- Performing Test FLAG_SUPPORTED_Wdoublepromotion - Success
-- Performing Test FLAG_SUPPORTED_Wparentheses
-- Performing Test FLAG_SUPPORTED_Wparentheses - Success
-- Performing Test FLAG_SUPPORTED_Wformatoverflow
-- Performing Test FLAG_SUPPORTED_Wformatoverflow - Success
-- Performing Test FLAG_SUPPORTED_Wunusedmacros
-- Performing Test FLAG_SUPPORTED_Wunusedmacros - Success
-- Performing Test FLAG_SUPPORTED_Wmissingvariabledeclarations
-- Performing Test FLAG_SUPPORTED_Wmissingvariabledeclarations - Failed
-- Performing Test FLAG_SUPPORTED_Wusedbutmarkedunused
-- Performing Test FLAG_SUPPORTED_Wusedbutmarkedunused - Failed
-- Performing Test FLAG_SUPPORTED_Wswitchenum
-- Performing Test FLAG_SUPPORTED_Wswitchenum - Success
-- Performing Test FLAG_SUPPORTED_fvisibilityhidden
-- Performing Test FLAG_SUPPORTED_fvisibilityhidden - Success
-- Configuring done (1.5s)
-- Generating done (0.0s)
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_CXX_COMPILER
-- Build files have been written to: /path/to/cJSON-1.7.18/build
make编译:
yoyo@yoyo:~/360Downloads/cJSON-1.7.18/build$ make -j8
[ 50%] Building C object CMakeFiles/cjson.dir/cJSON.c.o
[100%] Linking C shared library libcjson.so
[100%] Built target cjson
make install 安装:
yoyo@yoyo:~/360Downloads/cJSON-1.7.18/build$ make install
[100%] Built target cjson
Install the project...
-- Install configuration: ""
-- Installing: /path/to/cJSON-1.7.18/arm_install/include/cjson/cJSON.h
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/pkgconfig/libcjson.pc
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/libcjson.so.1.7.18
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/libcjson.so.1
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/libcjson.so
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/cmake/cJSON/cjson.cmake
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/cmake/cJSON/cjson-noconfig.cmake
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/cmake/cJSON/cJSONConfig.cmake
-- Installing: /path/to/cJSON-1.7.18/arm_install/lib/cmake/cJSON/cJSONConfigVersion.cmake
3. 移植到开发板
cp -arf /path/to/cJSON/arm_install/lib/ /usr/cJSON/