13.2 交叉编译hello world示例

NOTE:此示例代码可以在 https://github.com/dev-cafe/cmake-cookbook/tree/v1.0/chapter-13/recipe-01 中找到,其中包含一个C++示例。该示例在CMake 3.5版(或更高版本)中是有效的,并且已经在GNU/Linux、macOS和Windows上进行过测试。

这个示例中,我们将重用“Hello World”示例,并将代码从Linux或macOS交叉编译到Windows。换句话说,我们将在Linux或macOS上配置和编译代码,并生成Windows平台的可执行文件

准备工作

我们从hello world示例(hello-world.cpp)开始:

  1. #include <iostream>
  2. #include <omp.h>
  3. #include <string>
  4. int main(int argc, char *argv[])
  5. {
  6. std::cout << "number of available processors: " << omp_get_num_procs()
  7. << std::endl;
  8. std::cout << "number of threads: " << omp_get_max_threads() << std::endl;
  9. auto n = std::stol(argv[1]);
  10. std::cout << "we will form sum of numbers from 1 to " << n << std::endl;
  11. // start timer
  12. auto t0 = omp_get_wtime();
  13. auto s = 0LL;
  14. #pragma omp parallel for reduction(+ : s)
  15. for (auto i = 1; i <= n; i++)
  16. {
  17. s += i;
  18. }
  19. // stop timer
  20. auto t1 = omp_get_wtime();
  21. std::cout << "sum: " << s << std::endl;
  22. std::cout << "elapsed wall clock time: " << t1 - t0 << " seconds" << std::endl;
  23. return 0;
  24. }

我们还将使用与前一个示例相同的CMakeLists.txt

  1. # set minimum cmake version
  2. cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
  3. # project name and language
  4. project(recipe-01 LANGUAGES CXX)
  5. set(CMAKE_CXX_STANDARD 11)
  6. set(CMAKE_CXX_EXTENSIONS OFF)
  7. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  8. include(GNUInstallDirs)
  9. set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY
  10. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
  11. set(CMAKE_LIBRARY_OUTPUT_DIRECTORY
  12. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_LIBDIR})
  13. set(CMAKE_RUNTIME_OUTPUT_DIRECTORY
  14. ${CMAKE_BINARY_DIR}/${CMAKE_INSTALL_BINDIR})
  15. # define executable and its source file
  16. add_executable(hello-world hello-world.cpp)
  17. # we will print the system name in the code
  18. target_compile_definitions(hello-world
  19. PUBLIC
  20. "SYSTEM_NAME=\"${CMAKE_SYSTEM_NAME}\""
  21. )
  22. install(
  23. TARGETS
  24. hello-world
  25. DESTINATION
  26. ${CMAKE_INSTALL_BINDIR}
  27. )

为了交叉编译源代码,我们需要安装一个C++交叉编译器,也可以为C和Fortran安装一个交叉编译器。可以使用打包的MinGW编译器,作为打包的交叉编译器的替代方案。还可以使用MXE (M cross environment)从源代码构建一套交叉编译器:http://mxe.cc

具体实施

我们将按照以下步骤,在这个交叉编译的“hello world”示例中创建三个文件:

  1. 创建一个文件夹,其中包括hello-world.cppCMakeLists.txt

  2. 再创建一个toolchain.cmake文件,其内容为:

    1. # the name of the target operating system
    2. set(CMAKE_SYSTEM_NAME Windows)
    3. # which compilers to use
    4. set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
    5. # adjust the default behaviour of the find commands:
    6. # search headers and libraries in the target environment
    7. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
    8. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
    9. # search programs in the host environment
    10. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
  3. CMAKE_CXX_COMPILER设置为对应的编译器(路径)。

  4. 然后,通过将CMAKE_TOOLCHAIN_FILE指向工具链文件,从而配置代码(本例中,使用了从源代码构建的MXE编译器):

    1. $ mkdir -p build
    2. $ cd build
    3. $ cmake -D CMAKE_TOOLCHAIN_FILE=toolchain.cmake ..
    4. -- The CXX compiler identification is GNU 5.4.0
    5. -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++
    6. -- Check for working CXX compiler: /home/user/mxe/usr/bin/i686-w64-mingw32.static-g++ -- works
    7. -- Detecting CXX compiler ABI info
    8. -- Detecting CXX compiler ABI info - done
    9. -- Detecting CXX compile features
    10. -- Detecting CXX compile features - done
    11. -- Configuring done
    12. -- Generating done
    13. -- Build files have been written to: /home/user/cmake-recipes/chapter-13/recipe-01/cxx-example/build
  5. 现在,构建可执行文件:

    1. $ cmake --build .
    2. Scanning dependencies of target hello-world
    3. [ 50%] Building CXX object CMakeFiles/hello-world.dir/hello-world.cpp.obj
    4. [100%] Linking CXX executable bin/hello-world.exe
    5. [100%] Built target hello-world
  6. 注意,我们已经在Linux上获得hello-world.exe。将二进制文件复制到Windows上。

  7. 在WIndows上可以看到如下的输出:

    1. Hello from Windows
  8. 如你所见,这个二进制可以在Windows下工作。

工作原理

由于与目标环境(Windows)不同的主机环境(在本例中是GNU/Linux或macOS)上配置和构建代码,所以我们需要向CMake提供关于目标环境的信息,这些信息记录在toolchain.cmake文件中( https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling )。

首先,提供目标操作系统的名称:

  1. set(CMAKE_SYSTEM_NAME Windows)

然后,指定编译器:

  1. set(CMAKE_C_COMPILER i686-w64-mingw32-gcc)
  2. set(CMAKE_CXX_COMPILER i686-w64-mingw32-g++)
  3. set(CMAKE_Fortran_COMPILER i686-w64-mingw32-gfortran)

这个例子中,我们不需要检测任何库或头文件。如果必要的话,我们将使用以下命令指定根路径:

  1. set(CMAKE_FIND_ROOT_PATH /path/to/target/environment)

例如,提供MXE编译器的安装路径。

最后,调整find命令的默认行为。我们指示CMake在目标环境中查找头文件和库文件:

  1. set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
  2. set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)

在主机环境中的搜索程序:

  1. set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)

更多信息

有关各种选项的更详细讨论,请参见: https://cmake.org/cmake/help/latest/manual/cmake-toolchains.7.html#cross-compiling