【发布时间】:2016-07-11 18:49:02
【问题描述】:
我在使用 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 标志。
【问题讨论】:
-
看起来 CMake 无法检测到编译器支持
--sysroot选项。配置时 CMake 是否输出行The C compiler identification is GNU?顺便说一句,您在CMAKE_C_COMPILER变量定义中的“gnueabi-gcc”之前显示带有错误空格的工具链。您的原始工具链文件中没有这个空间,不是吗? -
配置时会检测到正确的 C 编译器
The C compiler identification is GNU 4.9.1.。 CMAKE_C_COMPILER 中的空间只是一个剪切和粘贴问题,我已经编辑了原始问题。 -
我不确定,但您尝试构建的 CMake 项目可能以某种方式修改了
CMAKE_C_FLAGS,因此--sysroot选项消失了。你能尝试用你的工具链构建简单的“Hello world”程序吗? -
只要我使用
--sysroot=/home/sifu/test-yocto/qemuarmdfs,工具链就可以与hello world完美配合。 -
糟糕,抱歉,我打算用简单的 CMake 项目(例如,通过
add_executable构建“Hellow world”程序)来测试 CMake 工具链文件。对于这样的项目,CMake 应该自动添加--sysroot选项,并且您可以在使用make V=1构建时看到它。