代码之家  ›  专栏  ›  技术社区  ›  Kagemand Andersen

CMake:C编译器无法编译简单的测试程序

  •  4
  • Kagemand Andersen  · 技术社区  · 6 年前

    错误消息:

    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编译器。有没有办法禁用此测试程序检查?

    解决方案是将以下内容添加到工具链文件:

    SET (CMAKE_C_COMPILER_WORKS 1)
    SET (CMAKE_CXX_COMPILER_WORKS 1)
    
    1 回复  |  直到 6 年前
        1
  •  47
  •   KamilCuk    3 年前

    CMake尝试使用“标准”(根据CMake认为是标准的)编译器选项编译可执行文件,并尝试运行该可执行文件,以查看编译器是否正常工作。可执行文件 is simple int main(int argc, char *argv[]) { return argc - 1; } .

    交叉编译时不能这样做。因为通常无法链接正确的标准C库,所以没有 printf _start _exit 或者类似的,将参数传递给 main 是否定义了实现,或者您需要一个链接器脚本,或者您的体系结构没有模拟器,因此无法在主机上运行交叉编译的源代码,等等。。。简单地说:您通常无法在主机上运行交叉编译的可执行文件,而且大多数情况下,即使是编译也很难完成。

    常见的解决方案是在 project() :

    set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
    

    因此,CMake将尝试编译一个静态库,而不是一个可执行文件,如前所述 here

    你可以设置 CMAKE_C_COMPILER_WORKS 并且它将省略每个 here ,但我觉得 CMAKE_TRY_COMPILE_TARGET_TYPE 这是一个更合适的解决方案。

        2
  •  1
  •   Rahul sharma    3 年前

    让我先解释一下我的问题

    当我删除 NDK Cmake 从…起 Sdk folder 然后运行我的应用程序 grable NDK 克马克 只有在应用程序运行时,当我再次尝试运行时,我才会出现此错误 The C Compiler is not able to compile a simple test program

    在我使用之前 ndkVersion "22.0.7026061" 然后换成这个 ndkVersion "21.1.6352462" 它成功了。

    我想这是 NDK

        3
  •  0
  •   TowardstheStars    3 年前

    如果使用CMake GUI,可以添加一个名为

    CMAKE_CXX_COMPILER_FORCED
    

    然后将其设置为True。

    这将跳过此生成的检查过程。