CMake: The C Compiler is not able to compile a simple test program

前端 未结 1 468
你的背包
你的背包 2020-12-28 13:55

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

相关标签:
1条回答
  • 2020-12-28 14:32

    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; }.

    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.

    The common solution is to set before project():

    set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
    

    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.

    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.

    0 讨论(0)
提交回复
热议问题