[CMake]CMake設置arm-linux-gcc交叉編譯器

標籤: cmake 交叉編譯器 arm-linux-gcc 分類: 開發工具/開發環境 
主機:Ubuntu18.04 
交叉編譯器:arm-linux-gcc 
CMake在ubuntu系統下默認使用系統的gcc、g++編譯器,編譯arm下的程序要使用arm-linux-gcc,須要對CMake進行設置(經過在CMakeLists.txt中指定交叉編譯器的方法)。 
在CMakeLists.txt一開始加入相關設置:html

告知當前使用的是交叉編譯方式,必須配置

SET(CMAKE_SYSTEM_NAME Linux)linux

指定C交叉編譯器,必須配置

或交叉編譯器使用絕對地址

SET(CMAKE_C_COMPILER 「arm-linux-gcc」)ubuntu

指定C++交叉編譯器

SET(CMAKE_CXX_COMPILER 「arm-linux-g++」)ide

不必定須要設置

指定交叉編譯環境安裝目錄…

SET(CMAKE_FIND_ROOT_PATH 「…」)工具

歷來不在指定目錄下查找工具程序

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)開發工具

只在指定目錄下查找庫文件

SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)ui

只在指定目錄下查找頭文件

SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)this

 

1.cmake 官網教程 https://cmake.org/cmake/help/v3.2/manual/cmake-toolchains.7.html?highlight=cmake_c_compiler#cross-compiling-for-linuxspa

2.Cross Compilingorm

If cmake(1) is invoked with the command line parameter-DCMAKE_TOOLCHAIN_FILE=path/to/file, the file will be loaded early to setvalues for the compilers.The CMAKE_CROSSCOMPILING variable is set to true when CMake iscross-compiling.

Cross Compiling for Linux
A typical cross-compiling toolchain for Linux has content suchas:

set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)

set(CMAKE_SYSROOT /home/devel/rasp-pi-rootfs)
set(CMAKE_STAGING_PREFIX /home/devel/stage)

set(tools /home/devel/gcc-4.7-linaro-rpi-gnueabihf)
set(CMAKE_C_COMPILER ${tools}/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER ${tools}/bin/arm-linux-gnueabihf-g++)

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
The CMAKE_SYSTEM_NAME is the CMake-identifier of the target platformto build for.

The CMAKE_SYSTEM_PROCESSOR is the CMake-identifier of the target architectureto build for.

The CMAKE_SYSROOT is optional, and may be specified if a sysrootis available.

The CMAKE_STAGING_PREFIX is also optional. It may be used to specifya path on the host to install to. The CMAKE_INSTALL_PREFIX is alwaysthe runtime installation location, even when cross-compiling.

The CMAKE_<LANG>_COMPILER variables may be set to full paths, or tonames of compilers to search for in standard locations. In cases where CMake doesnot have enough information to extract information from the compiler, theCMakeForceCompiler module can be used to bypass some of the checks.

CMake find_* commands will look in the sysroot, and the CMAKE_FIND_ROOT_PATHentries by default in all cases, as well as looking in the host system root prefix.Although this can be controlled on a case-by-case basis, when cross-compiling, itcan be useful to exclude looking in either the host or the target for particularartifacts. Generally, includes, libraries and packages should be found in thetarget system prefixes, whereas executables which must be run as part of the buildshould be found only on the host and not on the target. This is the purpose ofthe CMAKE_FIND_ROOT_PATH_MODE_* variables.

相關文章
相關標籤/搜索