[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一开始加入相关设置:

告知当前使用的是交叉编译方式,必须配置

SET(CMAKE_SYSTEM_NAME Linux)

指定C交叉编译器,必须配置

或交叉编译器使用绝对地址

SET(CMAKE_C_COMPILER “arm-linux-gcc”)

指定C++交叉编译器

SET(CMAKE_CXX_COMPILER “arm-linux-g++”)

不一定需要设置

指定交叉编译环境安装目录…

SET(CMAKE_FIND_ROOT_PATH “…”)

从来不在指定目录下查找工具程序

SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

只在指定目录下查找库文件

SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

只在指定目录下查找头文件

SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

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

2.Cross Compiling

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.

猜你喜欢

转载自www.cnblogs.com/lx17746071609/p/11915485.html