75

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.

Error message:

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

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)
Kagemand Andersen
  • 1,470
  • 2
  • 16
  • 30
  • 1
    "Is there a way to disable this test-program check?" - Yes. Easily googled: https://stackoverflow.com/questions/10599038/can-i-skip-cmake-compiler-tests-or-avoid-error-unrecognized-option-rdynamic – Tsyvarev Dec 05 '18 at 13:44
  • @Tsyvarev Ignored it because the accepted answer is from 2012 with no usable answer, but down at the bottom there's a line that seems to disable it. – Kagemand Andersen Dec 05 '18 at 13:53
  • If your last paragraph has intention answer your question, then it is not right place for it: on Stack Overflow we tend to separate *question* and *answer* posts. Instead, you may your answer it into the **answer post**: on Stack Overflow *self-answering* is [perfectly acceptible](https://stackoverflow.com/help/self-answer). – Tsyvarev Dec 05 '18 at 15:08
  • @Tsyvarev I know, but thought the 48 period also applied to posting it, not just to accepting it. Also someone might have a better solution, or just one with an explanation. – Kagemand Andersen Dec 05 '18 at 15:20

5 Answers5

79

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

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.

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 an executable, as explained in cmake docs CMAKE_TRY_COMPILE_TARGET_TYPE. This avoids running the linker and is intended for cross-compiling.

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Ah, that makes more sense then. Thought it might have been because the compiler was from ~2016, but, yeah, if it has trouble linking C libraries that would explain it. Thanks! – Kagemand Andersen Dec 05 '18 at 15:30
  • try_compile() does not try to execute the program but setting `CMAKE_TRY_COMPILE_TARGET_TYPE` is useful because you can avoid the linking step that is performed by `CMakeTestCCompiler.cmake` as you cannot easily add any special linker flags used by the `CMakeLists.txt` at this point. – fdk1342 Dec 05 '18 at 22:51
  • in this case it looks like `--sysroot=/usr/local/mipsisa32r2el/r23` is not a valid linker option and is causing the failure. – fdk1342 Dec 05 '18 at 22:52
  • Note that if you set CMAKE_TRY_COMPILE_TARGET_TYPE to STATIC_LIBRARY, CMAKE_SIZEOF_VOID_P will not be calculated/set, which many CMake scripts depend on. – Diarrhio Jan 24 '19 at 19:22
  • Got this error after upgrading to Ubuntu 18.04.2, your suggestion solved it, thanks! – GM1 Feb 20 '19 at 13:33
  • Hey. Just up-voting because of your helpful comment [here](https://stackoverflow.com/questions/68011128/where-to-find-usr-include-x11-extensions-xcomposite-h?noredirect=1#comment120208282_68011128) – WinEunuuchs2Unix Jun 16 '21 at 23:58
3

Ran into the same problem while re-compiling a project. Turned out that the compiler had been updated in the meantime.

After removing the build directory and creating it again, the compilation completed without errors.

ganzpopp
  • 364
  • 3
  • 12
  • This not worked, but when I read this, I realised that the problem is caused by copying CMake settings from antoher computer (settings for macOS moved to Windows)... – rejnok Mar 01 '23 at 20:42
1

If using CMake GUI, you can add a boolean entry named

CMAKE_CXX_COMPILER_FORCED

then set it to True.

This will skip checking process for this build.

1

Well this problem is really annoying, i faced this issue for 2 day now I got solution.

Let me explain my issue first

When I delete NDK and Cmake from Sdk folder and Run my application then grable install NDK and Cmake again. Only that time Application run and when try to run again i get this error The C Compiler is not able to compile a simple test program.

Before i was using ndkVersion "22.0.7026061" then change to thisndkVersion "21.1.6352462" and IT WORKED.

I think this is NDK problem and most of answer are outdated try this i hope this will HELP.

Rahul sharma
  • 1,492
  • 12
  • 26
0

In my case while working on ESP32-IDF Tools, Creating a project first and changing the directory from "CMakeTmp" to the created project directory resolved the problem. hope to help others.

idf.py create-project myproject
cd myproject
Ghm y
  • 1
  • 1