CMake:C 编译器无法编译简单的测试程序 [英] CMake: The C Compiler is not able to compile a simple test program

查看:131
本文介绍了CMake:C 编译器无法编译简单的测试程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Mips 处理器交叉编译 Azure IoT SDK C.使用旧版本的 CMake (2.8.12.2) 交叉编译相同 SDK 的旧版本工作得很好,所以我怀疑这是代码本身.我猜是 Mips GCC 编译器.

I am trying to cross-compile the Azure IoT SDK C for a Mips processor. Cross-compiling an older version of the same SDK using an older version of CMake (2.8.12.2) works just fine, so I doubt it's the code itself. I am guessing it's the Mips GCC compiler.

错误信息:

CMake Error at /usr/share/cmake-3.10/Modules/CMakeTestCCompiler.cmake:52 (message):
  The C compiler

    "/usr/local/mipsisa32r2el/r23/bin/mipsisa32r2el-axis-linux-gnu-gcc"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /home/axis/azure-iot-sdk-c/cmake/iotsdk_linux/CMakeFiles/CMakeTmp

    Run Build Command:"/usr/bin/make" "cmTC_2cc84/fast"
    /usr/bin/make -f CMakeFiles/cmTC_2cc84.dir/build.make CMakeFiles/cmTC_2cc84.dir/build
    make[1]: Entering directory '/home/axis/azure-iot-sdk-c/cmake/iotsdk_linux/CMakeFiles/CMakeTmp'
    Building C object CMakeFiles/cmTC_2cc84.dir/testCCompiler.c.o
    /usr/local/mipsisa32r2el/r23/bin/mipsisa32r2el-axis-linux-gnu-gcc --sysroot=/usr/local/mipsisa32r2el/r23    -o CMakeFiles/cmTC_2cc84.dir/testCCompiler.c.o   -c /home/axis/azure-iot-sdk-c/cmake/iotsdk_linux/CMakeFiles/CMakeTmp/testCCompiler.c
    Linking C executable cmTC_2cc84
    /usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_2cc84.dir/link.txt --verbose=1
    /usr/local/mipsisa32r2el/r23/bin/mipsisa32r2el-axis-linux-gnu-gcc --sysroot=/usr/local/mipsisa32r2el/r23      -rdynamic CMakeFiles/cmTC_2cc84.dir/testCCompiler.c.o  -o cmTC_2cc84 
    /usr/local/mipsisa32r2el/r23/lib/gcc/mipsisa32r2el-axis-linux-gnu/4.7.2/../../../../mipsisa32r2el-axis-linux-gnu/bin/ld: this linker was not configured to use sysroots
    collect2: error: ld returned 1 exit status
    CMakeFiles/cmTC_2cc84.dir/build.make:97: recipe for target 'cmTC_2cc84' failed
    make[1]: *** [cmTC_2cc84] Error 1
    make[1]: Leaving directory '/home/axis/azure-iot-sdk-c/cmake/iotsdk_linux/CMakeFiles/CMakeTmp'
    Makefile:126: recipe for target 'cmTC_2cc84/fast' failed
    make: *** [cmTC_2cc84/fast] Error 2

不幸的是,我坚持使用我拥有的 Mips GCC 编译器.有没有办法禁用这个测试程序检查?

Unfortunately, I am stuck with the Mips GCC compiler I have. Is there a way to disable this test-program check?

解决方案是将这些添加到工具链文件中:

Solution was to add these to the toolchain-file:

SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)

推荐答案

CMake 尝试使用标准"编译可执行文件(根据 CMake 认为的标准)编译器选项并尝试运行该可执行文件,以便查看编译器是否正常工作.可执行文件很简单,就像intmain(int argc, char *argv[]) { return argc - 1;}.

CMake tries to compile an executable using "standard" (as per what CMake thinks is standard) compiler options and tries to run that executable, so to see if the compiler is working. The executable is simple like int main(int argc, char *argv[]) { return argc - 1; }.

交叉编译时不能这样做.因为通常你不能链接到一个合适的 C 标准库,你没有 printf,或 _start_exit 或类似的,传递main 的参数是实现定义的,或者你需要一个特殊的链接器脚本,或者你的架构没有模拟器,所以不能在主机上运行交叉编译的源代码等等......简单地说:您通常无法在主机上运行交叉编译的可执行文件,而且大多数时候甚至编译也很难做到.

You can't do that when cross-compiling. Because usually you can't link with a proper C standard library, you don't have printf, or _start or _exit or similar, passing arguments to main is implementation-defined, or you need a special linker script, or there's no emulator for your architecture, so can't run cross-compiled source on the host, etc... Simply: you usually can't run the cross-compiled executable on the host, and most of the time even the compilation is hard enough to do.

常见的解决方法是在project()之前设置:

The common solution is to set before project():

set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")

这样 CMake 将尝试编译静态库而不是可执行文件,如 cmake 文档 CMAKE_TRY_COMPILE_TARGET_TYPE.这避免了运行链接器,并用于交叉编译.

So that CMake will try to compile a static library not an executable, as explained in cmake docs CMAKE_TRY_COMPILE_TARGET_TYPE. This avoids running the linker and is intended for cross-compiling.

您可以设置CMAKE_C_COMPILER_WORKS,它会省略CMakeTestCCompiler.cmake,但 CMAKE_TRY_COMPILE_TARGET_TYPE 是更合适的解决方案.

You can set CMAKE_C_COMPILER_WORKS and it will omit the check in CMakeTestCCompiler.cmake, but CMAKE_TRY_COMPILE_TARGET_TYPE is a more proper solution.

这篇关于CMake:C 编译器无法编译简单的测试程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆