1、准备
在使用CMake生成交叉编译的Makefile之前,还是先回顾一下CMake的基本使用方法:Linux Makefile:使用CMake生成Makefile文件。以最简单的项目直接看交叉编译的例子:
book@Ubuntu:~/work/cmake$ tree
.
├── CMakeLists.txt
├── main.c
└── toolchains.cfg
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
project(demo)
add_executable(main main.c)
main.c:
#include <stdio.h>
int main(void)
{
printf("Hello World\n");
return 0;
}
toolchains.cfg:
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
文件命名随意,内容才是重点,选项如下:
- CMAKE_SYSTEM_NAME:必须存在,操作系统名称,如Linux、Android、WindowsCE等,没有操作系统则为Generic;
- CMAKE_SYSTEM_PROCESSOR:处理器架构,这里使用的是arm架构,可以省略;
- CMAKE_C_COMPILER:设置C编译器;
- CMAKE_CXX_COMPILER:设置C++编译器;
- CMAKE_FIND_ROOT_PATH:设置它的值为一系列目录(结合后面几个选项一起分析);
- CMAKE_FIND_ROOT_PATH_MODE_PROGRAM:针对于FIND_PROGRAM(),如果设为NEVER,CMAKE_FIND_ROOT_PATH就不会起作用;如果为ONLY则只从CMAKE_FIND_ROOT_PATH里查找;如果为BOTH则优先从CMAKE_FIND_ROOT_PATH查找,再到默认目录下查找;
- CMAKE_FIND_ROOT_PATH_MODE_LIBRARY:针对于FIND_LIBRARY(),表示链接的库,所以就需要设置成ONLY来确保交叉编译环境的库;
- CMAKE_FIND_ROOT_PATH_MODE_INCLUDE:针对于FIND_PATH()和FIND_FILE(),一般也是选ONLY。
2、使用
- 用法1:执行cmake命令时使用
-DCMAKE_TOOLCHAIN_FILE=file
指定(灵活,推荐) - 用法2:在CMakeLists.txt开头添加
include(file)
book@Ubuntu:~/work/cmake$ ls
CMakeLists.txt main.c toolchains.cfg
book@Ubuntu:~/work/cmake$
book@Ubuntu:~/work/cmake$ cmake -DCMAKE_TOOLCHAIN_FILE=toolchains.cfg .
-- The C compiler identification is GNU 4.9.4
-- The CXX compiler identification is GNU 4.9.4
-- Check for working C compiler: /home/book/work/imx6ull/system/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/book/work/imx6ull/system/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-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: /home/book/work/imx6ull/system/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-c++
-- Check for working CXX compiler: /home/book/work/imx6ull/system/gcc-linaro-4.9.4-2017.01-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/book/work/cmake
book@Ubuntu:~/work/cmake$
book@Ubuntu:~/work/cmake$
book@Ubuntu:~/work/cmake$ make
Scanning dependencies of target main
[ 50%] Building C object CMakeFiles/main.dir/main.c.o
[100%] Linking C executable main
[100%] Built target main
book@Ubuntu:~/work/cmake$
book@Ubuntu:~/work/cmake$
book@Ubuntu:~/work/cmake$ file main
main: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.31, BuildID[sha1]=0fba62f3e77096a03d750742c111f0f36ab9334a, not stripped
可以见到,make编译生成的可执行文件运行环境已经不是主机的x86-64架构了,而是前面设定用于32位ARM架构的处理器。至此,使用CMake生成用于交叉编译的Makefile已经完成了。
参考:
快速上手:使用CMake交叉编译Arm Linux程序 – CSDN
选项说明:CMake交叉编译配置 – 博客园
参考示例:使用 cmake 进行交叉编译 – 博客园
官方说明:cmake-toolchains(7)