交叉编译jsoncpp静态库

许多实用cmake的开源项目在移植到其他平台时,还是比较麻烦的。

这里以jsoncpp为例。


首先,我本机的交叉编译工具及目录是:

# which arm-linux-gnueabihf-gcc
/root/board/tools/gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/bin/arm-linux-gnueabihf-gcc

从github上clone代码:

# git clone https://github.com/open-source-parsers/jsoncpp.git

编写tool chain file,可以参考这个:

tool chain file实例

根据自己的配置改下如下:

# this one is important
SET(CMAKE_SYSTEM_NAME Linux)
#this one not so much
SET(CMAKE_SYSTEM_VERSION 1)

# specify the cross compiler
SET(CMAKE_C_COMPILER   /root/board/tools/gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/bin/arm-linux-gnueabihf-gcc)
SET(CMAKE_CXX_COMPILER /root/board/tools/gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/bin/arm-linux-gnueabihf-g++)
SET(CMAKE_STRIP /root/board/tools/gcc-linaro-arm-linux-gnueabihf-4.7-2013.03-20130313_linux/bin/arm-linux-gnueabihf-strip)

# where is the target environment 
SET(CMAKE_FIND_ROOT_PATH  /usr/arm-linux-gnueabihf)

# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
我的tool chain file保存在~/ 下,名字为toolchain.cmake。

然后进入jsoncpp目录:

# cd jsoncpp
# mkdir build
# cd build

在build目录下执行:

cmake -DCMAKE_BUILD_TYPE=None -DJSONCPP_LIB_BUILD_STATIC=ON -DJSONCPP_LIB_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX=/usr/arm-linux-gnueabihf -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake -G "Unix Makefiles" ..

如果出现错误,请检测tool chain file和上面命令参数和你本机是否相符。

在build下生成:

CMakeCache.txt  cmake_install.cmake  include   pkg-config
CMakeFiles      CTestTestfile.cmake  Makefile  src

在build目录下执行make:

# make
Scanning dependencies of target jsoncpp_lib_static
[ 11%] Building CXX object src/lib_json/CMakeFiles/jsoncpp_lib_static.dir/json_reader.cpp.o
[ 22%] Building CXX object src/lib_json/CMakeFiles/jsoncpp_lib_static.dir/json_value.cpp.o
/mnt/win/src.emb/Scanner/vendor/jsoncpp/src/lib_json/json_value.cpp: In copy constructor ‘Json::Value::CZString::CZString(const Json::Value::CZString&)’:
/mnt/win/src.emb/Scanner/vendor/jsoncpp/src/lib_json/json_value.cpp:275:78: warning: conversion to ‘unsigned char:2’ from ‘unsigned int’ may alter its value [-Wconversion]
[ 33%] Building CXX object src/lib_json/CMakeFiles/jsoncpp_lib_static.dir/json_writer.cpp.o
[ 44%] Linking CXX static library libjsoncpp.a
[ 44%] Built target jsoncpp_lib_static
Scanning dependencies of target jsontestrunner_exe
[ 55%] Building CXX object src/jsontestrunner/CMakeFiles/jsontestrunner_exe.dir/main.cpp.o
[ 66%] Linking CXX executable jsontestrunner_exe
[ 66%] Built target jsontestrunner_exe
Scanning dependencies of target jsoncpp_test
[ 77%] Building CXX object src/test_lib_json/CMakeFiles/jsoncpp_test.dir/jsontest.cpp.o
[ 88%] Building CXX object src/test_lib_json/CMakeFiles/jsoncpp_test.dir/main.cpp.o
[100%] Linking CXX executable jsoncpp_test

libjsoncpp.a已经生成。



你可能感兴趣的:(工具)