CMake: The C Compiler is not able to compile a simple test program - 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.
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)

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.

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.

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.

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.

Related

MinGW C compiler "not able to compile a simple test program"

I want to run MinGW as a C compiler. MinGW has been installed from Chocolatey. Invocation is from gitlab-runner through CMake. This fails with
$ cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DLIB_MAN=OFF -DCMAKE_INSTALL_PREFIX=C:\opt\x64 -B. ..
-- The C compiler identification is unknown
-- Check for working C compiler: C:/ProgramData/chocolatey/bin/x86_64-w64-mingw32-gcc.exe
-- Check for working C compiler: C:/ProgramData/chocolatey/bin/x86_64-w64-mingw32-gcc.exe -- broken
CMake Error at C:/Program Files/CMake/share/cmake-3.16/Modules/CMakeTestCCompiler.cmake:60 (message):
The C compiler
"C:/ProgramData/chocolatey/bin/x86_64-w64-mingw32-gcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: C:/gitlab-runner/builds/zjE1Mkfg/0/mlz/kww/build/CMakeFiles/CMakeTmp
Run Build Command(s):C:/ProgramData/chocolatey/bin/mingw32-make.exe cmTC_ffa53/fast && C:/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin/mingw32-make -f CMakeFiles\cmTC_ffa53.dir\build.make CMakeFiles/cmTC_ffa53.dir/build
mingw32-make[1]: Entering directory 'C:/gitlab-runner/builds/zjE1Mkfg/0/mlz/kww/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_ffa53.dir/testCCompiler.c.obj
C:\ProgramData\chocolatey\bin\x86_64-w64-mingw32-gcc.exe -o CMakeFiles\cmTC_ffa53.dir\testCCompiler.c.obj -c C:\gitlab-runner\builds\zjE1Mkfg\0\mlz\kww\build\CMakeFiles\CMakeTmp\testCCompiler.c
mingw32-make[1]: *** [CMakeFiles\cmTC_ffa53.dir\build.make:65: CMakeFiles/cmTC_ffa53.dir/testCCompiler.c.obj] Error 1
mingw32-make[1]: Leaving directory 'C:/gitlab-runner/builds/zjE1Mkfg/0/mlz/kww/build/CMakeFiles/CMakeTmp'
mingw32-make: *** [Makefile:120: cmTC_ffa53/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:6 (project)
-- Configuring incomplete, errors occurred!
See also "C:/gitlab-runner/builds/zjE1Mkfg/0/mlz/kww/build/CMakeFiles/CMakeOutput.log".
See also "C:/gitlab-runner/builds/zjE1Mkfg/0/mlz/kww/build/CMakeFiles/CMakeError.log".
ERROR: Job failed: exit status 1
From various web sources I understand that MinGW is bound to fail if sh.exe is present on the system [1]. However, sh.exe comes with several applications, among them Git and Visual Studio, which I cannot remove. Somewhere it's suggested to add -DCMAKE_SH="CMAKE_SH-NOTFOUND", which, however, has no effect.
I've seen a related question from 2015 [2], which however involves CLion. The only upvoted answer contains several broken links - altogether, that thread isn't helpful.
How to proceed? Is there a chance to work around the sh.exe problem?
Other advice on the web suggests to use -G "MSYS Makefiles". However, the CMake documentation [3] seems to rule this out: "not compatible with a Windows command prompt. To build under a Windows command prompt, use the MinGW Makefiles generator."
[1] https://stackoverflow.com/a/7411982/1017348
[2] CLion: Error:The C compiler "C:/MinGW/bin/gcc.exe" is not able to compile a simple test program
[3] https://cmake.org/cmake/help/latest/generator/MSYS%20Makefiles.html#generator:MSYS%20Makefiles per CMake 3.16.1
After long experimentation, I conclude that the problem comes from Chocolatey's packaging of MinGW. The problem is gone after I
uninstalled the MinGW package from Chocolatey,
reinstalled MinGW using the original MinGW installer,
installed the Ninja Chocolatey package,
inserted C:\MinGW\bin on top of PATH.
$ cmake -G "Ninja" -DLIB_MAN=OFF -DCMAKE_INSTALL_PREFIX=C:\opt\x64 -B. ..
-- The C compiler identification is GNU 8.2.0
-- Check for working C compiler: C:/MinGW/bin/gcc.exe
-- Check for working C compiler: C:/MinGW/bin/gcc.exe -- works
Also note:
There is no need for -DCMAKE_C_COMPILER=...
I did not try hard to get -G "MinGW Makefile" working; -G Ninja works just fine.
The sh.exe is only a problem when you run CMake the first time when trying to generate the project. You can rerun CMake again and CMake will continue with normal processing to generate the project (tested in version 3.16). I saw a patch that removes this check and should be part of a future release of CMake.
It's already mentioned that gcc is installed incorrectly. That always seems to be the case when anyone mentions that gcc fails with an Error 1 message. It should be noted that Error 1 is not from CMake. CMake is just gathering the output from when it runs mingw32-make to build the sample project.
The first message in the output -- The C compiler identification is unknown is a pretty clear message that your compiler isn't working. All of the output for this step is in the log and error files that CMake generates in the build folder. You'll be able to see how it invoked the compiler and with which options and the output from the compiler.

Make returns `mpif90: No such file or directory`

I'm trying to work with a program that a physicist wrote. I'm on a MacOS, and I've downloaded all the programs and libraries that I was told was necessary for the program to compile and run (GCC, MPI, using the version of MAKE that is already on Macs). I opened the makefile, and I was told to write
F90 = mpif90
and then compile the program using
make IO=txt
but I get this message:
mpif90 -g -O3 -c mod_input.f90
make: mpif90: No such file or directory
make: *** [mod_input.o] Error 1
I have no idea what this means, and what to do to get this to work.
Take a look here:
http://www.owsiak.org/?p=3492
This article describes how to get, install and configure MPI (Open MPI flavor) at macOS. After installing it, you should get mpif90 as well.

OpenELEC: Bootstrap GCC with libatomic

I am trying to bootstrap GCC during OpenELEC compilation. I need to add libatomic for the target system so as to compile some packages.
When I try to add libatomic for the target system, I get a compilation error with:
/home/mathieu/tmp/OpenELEC.tv/build.OpenELEC-ci20.mips-8.0-devel/toolchain/mipsel-openelec-linux-gnu/bin/ld: cannot find crti.o: No such file or directory
/home/mathieu/tmp/OpenELEC.tv/build.OpenELEC-ci20.mips-8.0-devel/toolchain/mipsel-openelec-linux-gnu/bin/ld: cannot find -lc
/home/mathieu/tmp/OpenELEC.tv/build.OpenELEC-ci20.mips-8.0-devel/toolchain/mipsel-openelec-linux-gnu/bin/ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status
Using the following package.mk file:
https://github.com/malaterre/OpenELEC.tv/blob/fd5a5558104ed38aee1c53bb6d31ba73e8eb6e57/packages/lang/gcc/package.mk
If that matter I am targetting a MIPS system, specifically the Creator CI20:
https://github.com/malaterre/OpenELEC.tv/blob/master/config/arch.mips
I am not clear about the OpenELEC build system, but it seems as if the host compiler and target compiler are build at the same time, while I would need to build binutils in between:
http://preshing.com/20141119/how-to-build-a-gcc-cross-compiler/
You might need to have a GCC built without libatomic to build one with libatomic. The intermediary compiler will serve to build your CRT (if you do not have it already) and libatomic, so that a second compiler can use these "prebuilt" things.
That was not hard at all. Basically one need to create first a minimal gcc (bootstrap) and then build the full one with libatomic:
https://github.com/malaterre/OpenELEC.tv/blob/70dbe25c1647f01eb83c108939c470437a2db259/packages/lang/gcc/package.mk

Can't make FestVox compile due to missing -leststring and missing libeststring.a

So I have installed / compiled speech_tools, and Festival (2.3) using Cygwin on my Win8.1 machine to the point that I can successfully produce speech using this command:
echo "hello world" | \src\main\festival --tts
The next step is for me to get FestVox running. I have downloaded FestVox 2.6 and I have run ./configure; however, the 'make' step is giving me trouble, producing this error:
gcc -O3 -Wall -o phonealign phonealign_main.o -LC:/cygwin64/Festival/build
/speech_tools/lib -lestools -lestbase -leststring -lncurses -lstdc++ -lm -lwinmm -luser32
/usr/bin/ld: cannot find -leststring
collect2: error: ld returned 1 exit status
Makefile:80: recipe for target 'phonealign' failed
So, I looked at my Makefile at where it might be trying to look for this file, and it looks like in that directory (build\speech_tools\lib) I am missing a libeststring.a partner for my libeststring.lib. Both libestbase and libestools have .lib and .a files in that directory.
At what step did I go wrong?? Should a libeststring.a have been created at some point??? When? How can I fix this?
I think the problem is that you should use compiler in Windows instead of gcc within Cygwin. The role in Cygwin for building Festival is to run configure to generate a Makefile for VC. Then run nmake in Windows command line not make within Cygwin.
Cygwin cannot build a native Windows application like what MINGW does. Application build in Cygwin can only run within Cygwin.
*.a is the static library for Linux, which is built by gcc. *.lib is the static library for Windows, which is built by VC.
So I suggest you taking a look at README, INSTALL files in FestVox. To find whether there is description for make a Makefile for Windows like process "3. Make makefile for VC in Cygwin" in my document (http://www.eguidedog.net/doc_build_win_festival.php)
Cameron

Building GCC cross compiler (from "Linux" to "Windows")

I want to build "gcc cross-compiler" to compile "c/c++" applications on "Linux" environment but for "Windows" target.
I have made this so far:
Installed the necessary tools and packages for building GCC listed on "Prerequisites for GCC" page.
Downloaded required sources:
"gcc-core-4.4.1", "gcc-g++-4.4.1", "binutils-2.19.1", "w32api-3.13-mingw32", "mingwrt-3.16-mingw32"
Created this directory hierarchy:
"${HOME}/gcc/" - for final cross-compiler
"${HOME}/src/" - for sources
"${HOME}/src/build-binutils/i386-mingw32/" - for building binutils to "i386-mingw32" target
"${HOME}/src/build-gcc/i386-mingw32/" - for building gcc to "i386-mingw32" target
Builded binutils package:
cd "${HOME}/src/build-binutils/i386-mingw32/"
../../binutils-2.19.1/configure --prefix="${HOME}/gcc" --target=i386-mingw32 --disable-nls
make
make install
Copied "w32api" and "mingwrt" headers to the install directory:
cp -R "${HOME}/src/w32api-3.13-mingw32/include" "${HOME}/gcc/i386-mingw32"
cp -R "${HOME}/src/mingwrt-3.16-mingw32/include" "${HOME}/gcc/i386-mingw32"
And now when I am trying to build the "c (only) cross-compiler":
cd "${HOME}/src/build-gcc/i386-mingw32/"
../../gcc-4.4.1/configure --prefix="${HOME}/gcc" --target=i386-mingw32 --enable-languages=c --with-headers="${HOME}/gcc/i386-mingw32/include" --disable-nls
make<br>
it was building something about 4 minutes and then gives me these errors:
${HOME}/gcc/i386-mingw32/bin/ld: dllcrt2.o: No such file: No such file or directory
collect2: ld returned 1 exit status
make[2]: *** [libgcc_s.dll] Error 1
make[2]: Leaving directory `${HOME}/src/build-gcc/i386-mingw32/i386-mingw32/libgcc'
make[1]: *** [all-target-libgcc] Error 2
make[1]: Leaving directory `${HOME}/src/build-gcc/i386-mingw32'
make: *** [all] Error 2
From that error message I really don't know what to do now :-((( .
Does anybody know where is the problem?
Thanks.
That's actually OK: the way things go, you need to
build binutils
install headers
build the a partial C compiler: enough to create object files, but not enough to link
build the win32api and mingw runtime (which includes your missing dllcrt2.o)
build a complete C compiler (and other front-ends, such as C++, Fortran, Ada, whatever, if you want them)
You have successful performed step 3 above; it fails building libgcc (which is a GCC support library), but that means the C compiler core is functionnal (although it won't be able to link, it can still create valid object files). You can check that by looking at the gcc/xgcc file in your GCC build directory.
So, you need to go to the next step, not worrying about your current failure.
(To actuall install the partial C compiler, you should run make with the -k option, to have it do it best, even in the face of errors. For example, use make -k install.)
There are precompiled cross-compilers of MinGW-w64 available.
This allows to compile native 32- and 64-bit Windows binaries from Linux, a two minute tutorial is available at http://www.blogcompiler.com/2010/07/11/compile-for-windows-on-linux/
Just in case you don't want to spend a lot of time trying to build it yourself.
I grepped through the MinGW sources, and found that dllcrt2.o is something built off the mingwrt package. I assume you have to compile and install that, not just copy the headers?

Resources