我在使用 CMAKE 设置交叉编译时遇到了一些麻烦。我正在使用的工具链是在 yocto 中创建的,它在 cmake 之外完美运行。
我按照教程设置了以下工具链文件:
SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_SYSTEM_VERSION 1)
SET(CMAKE_SYSTEM_PROCESSOR arm)
# specify the cross compiler
SET(tools /opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr)
SET(CMAKE_C_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
SET(CMAKE_CXX_COMPILER ${tools}/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc)
# set sysroot
SET(CMAKE_SYSROOT /home/sifu/test-yocto/qemuarmdfs)
#SET(CMAKE_FIND_ROOT_PATH /home/sifu/test-yocto/qemuarm)
# search for programs in the build host directories
SET(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
# for libraries and headers in the target directories
SET(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
SET(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
并在运行cmake时得到以下错误
The C compiler
"/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp
Run Build Command:/usr/bin/make "cmTryCompileExec4012536451/fast"
/usr/bin/make -f CMakeFiles/cmTryCompileExec4012536451.dir/build.make
CMakeFiles/cmTryCompileExec4012536451.dir/build
make[1]: Entering directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'
/usr/bin/cmake -E cmake_progress_report
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/CMakeFiles
1
Building C object
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o
/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -o
CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o -c
/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp/testCCompiler.c
Linking C executable cmTryCompileExec4012536451
/usr/bin/cmake -E cmake_link_script
CMakeFiles/cmTryCompileExec4012536451.dir/link.txt --verbose=1
/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/bin/arm-poky-linux-gnueabi/arm-poky-linux-gnueabi-gcc
-O2 -pipe -g -feliminate-unused-debug-types -Wl,-O1 -Wl,--hash-style=gnu
-Wl,--as-needed CMakeFiles/cmTryCompileExec4012536451.dir/testCCompiler.c.o
-o cmTryCompileExec4012536451 -rdynamic
... (A lot of ld errors similar to the one below)
/opt/poky/1.7.1/sysroots/x86_64-pokysdk-linux/usr/libexec/arm-poky-linux-gnueabi/gcc/arm-poky-linux-gnueabi/4.9.1/ld:
cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
make[1]: Leaving directory
`/home/sifu/Projects/mv/doublepump-single-pump-sw.ss016m21_swapp/cc/CMakeFiles/CMakeTmp'
如果我使用 –sysroot=/home/sifu/test-yocto/qemuarmdfs 手动运行上面日志中描述的 gcc 命令,它对我有用。为什么在工具链文件中添加 sysroot 的路径时 cmake 不使用 sysroot 标志。
原文由 user1178014 发布,翻译遵循 CC BY-SA 4.0 许可协议
我只是自己打了这个。问题是 CMake 仅将 CMAKE_SYSROOT 传递给编译器,这些编译器指定用于接收 sysroot 的命令行标志是什么,并且在 CMakeDetermineCompilerId 的第一次探测期间还不知道它正在调用什么编译器。一旦它检测到它是 GNU,Modules/Compiler/GNU.cmake 就会做
设置完成后,CMake 将开始传递“–sysroot=${CMAKE_SYSROOT}”
这就是为什么要运行多次;尽管“检查工作的 C 编译器”第一次失败,但 CMake 仍然检测到失败的编译器是 GNU 的一种风格并缓存了它。所以在第二次尝试时,它加载了 GNU.cmake, 确实 通过
--sysroot
,并且一切正常(然后它有同样的失败,但对于 CXX)。在第三次尝试时,CXX 也可以工作,并且事情实际上已经过去了。您可以通过多种方式解决此问题,但我不确定什么是最好的
if((NOT CMAKE_C_COMPILER_ID) OR (NOT CMAKE_CXX_COMPILER_ID)) 设置(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) endif()
设置(CMAKE_C_COMPILER_ID GNU 缓存字符串“”) 设置(CMAKE_CXX_COMPILER_ID GNU 缓存字符串“”)
设置(CMAKE_C_COMPILE_OPTIONS_SYSROOT“–sysroot=”)设置(CMAKE_CXX_COMPILE_OPTIONS_SYSROOT“–sysroot=”)
我不确定哪个是最好的解决方法,我认为整个事情都可以说是一个 CMake 错误,因为它确实检测到编译器 id 很好(使用 -c,因此不需要链接),它只是没有重新加载 GNU .cmake 基于 CMAKE_DETERMINE_COMPILER_ID 的结果,然后检查编译器是否工作。