2020/9/30

CMake cross-build -- served

用 CMake 來做 cross-build
-- 其實是因為大多使用 CMake 的 project 都是 host - target 同一系統,所以 detect, config 都是自動。但是現在要 build for target,就要讓 CMake 可以做 cross build.

看起來,是要用 -DCMAKE_TOOLCHAIN_FILE=maytoolchain.cmake
然後寫 mytoolchain.cmake
裡面指定toolchain path, name 和一堆 flag, library 等...

然後goole CMAKE_TOOLCHAIN_FILE 可以找一堆 sample,,



cross build served..

準備好 toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(TOOLCHAIN_PATH /usr/local/linaro-aarch64-2018.08-gcc8.2)
set(CMAKE_C_COMPILER ${TOOLCHAIN_PATH}/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PATH}/bin/aarch64-linux-gnu-g++)

run cmake....
served.build$ cmake -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake ../served
Error:
CMake Error at /usr/share/cmake-3.10/Modules/FindBoost.cmake:1947 (message):
  Unable to find the requested Boost libraries.

  Boost version: 1.65.1

  Boost include path: /usr/include

  Could not find the following Boost libraries:

          boost_system

  No Boost libraries were found.  You may need to set BOOST_LIBRARYDIR to the
  directory containing Boost libraries or BOOST_ROOT to the location of
  Boost.

in case cross-build boost: .. 結果沒用到,target system 的 boost 1.66.0 也可以用。所以不用 port boost

依照Error message,把 target system 的 boost package folder copy 到/tmp
要安排一下,把lib 和 header(include) 依照下面方式放好..
/tmp/boost/include/boost
/tmp/boost/lib
並且把 修改 toolchain.cmake,加入
set(BOOST_ROOT /tmp/boost)
一樣..
served.build$ cmake -DCMAKE_TOOLCHAIN_FILE=~/toolchain.cmake ../served
-- The C compiler identification is GNU 8.2.1
-- The CXX compiler identification is GNU 8.2.1
-- Check for working C compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-g++
-- Check for working CXX compiler: /usr/local/linaro-aarch64-2018.08-gcc8.2/bin/aarch64-linux-gnu-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /usr/share/cmake-3.10/Modules/FindBoost.cmake:801 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:907 (_Boost_COMPONENT_DEPENDENCIES)
  /usr/share/cmake-3.10/Modules/FindBoost.cmake:1558 (_Boost_MISSING_DEPENDENCIES)
  CMakeLists.txt:71 (FIND_PACKAGE)


-- Boost version: 1.66.0
-- Found the following Boost libraries:
--   system
-- Could NOT find RAGEL (missing: RAGEL_EXECUTABLE) 
-- Could NOT find RAGEL (missing: RAGEL_EXECUTABLE) 
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Performing Test COMPILER_SUPPORTS_CXX11
-- Performing Test COMPILER_SUPPORTS_CXX11 - Success
-- Performing Test COMPILER_SUPPORTS_CXX0X
-- Performing Test COMPILER_SUPPORTS_CXX0X - Success
-- Configuring done
-- Generating done
-- Build files have been written to: /home/charles-chang/githubgitlab/served.build
成功了...

到 served/bin/ 下 check file ,都是 ARM aarch64 ELF 沒錯。

放到 target board 上 run ..
  • 把 served/lib/ 下的so copy 到 /lib/ 下
  • 有用到 boost_system.so,所以要記得把 target build system 的 so 也 copy 到 /lib/ 下

沒有留言:

張貼留言