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

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

问题描述

我正在尝试针对Mips处理器交叉编译Azure IoT SDKC.使用较旧版本的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认为是标准的)编译器选项来编译可执行文件,并尝试运行该可执行文件,以便了解如果编译器正在工作.可执行文件很简单,通常类似于int main(int argc) { 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, usually like int main(int argc) { return argc - 1; }.

交叉编译时不能这样做.因为通常您不能链接适当的标准C库,所以您没有printf_start_exit或类似内容,因此将参数传递给main是实现定义的,或者您需要链接描述文件或您不能在主机上运行交叉编译的源代码,等等...简单:您通常不能在主机上运行交叉编译的可执行文件,并且在大多数情况下,即使编译起来也很难做到.

You can't do that when cross-compiling. Because usually you can't link a proper standard C library, you don't have printf, or _start or _exit or similar, passing arguments to main is implementation defined, or you need a linker script or you can't run cross-compiled source on host, etc... Simply: you usually can't run cross-compiled executable on 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尝试编译静态库而不是可执行文件,如这里.这样可以避免运行链接程序,并且可以用于交叉编译.

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

您可以设置CMAKE_C_COMPILER_WORKS,它会根据

You can set CMAKE_C_COMPILER_WORKS and it will omit the check per here, but I feel the CMAKE_TRY_COMPILE_TARGET_TYPE is a more proper solution.

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

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