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:

cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -B. ..

This fails with

The C compiler
    "C:/ProgramData/chocolatey/bin/x86_64-w64-mingw32-gcc.exe"
is not able to compile a simple test program

From the following lines we learn that the test program is called testCCompiler.c, but not why the compilation fails.

From various web sources I understand that MinGW is bound to fail if sh.exe is present on the system. 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.

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 seems to rule this out: “not compatible with a Windows command prompt. To build under a Windows command prompt, use the MinGW Makefiles generator.”

((disclosure: cross-posting from https://stackoverflow.com/questions/59355908))

The sh.exe is only a problem when you run CMake the first time when trying to build the project. You can rerun it afterwords and CMake will continue with normal processing. I saw a patch that removes this check and should be part of a future release of CMake.

On SO it was shown that gcc was printing an error message of Error 1.

    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

I have doubts that x86_64-w64-mingw32-gcc is installed correctly. You can paste the command, 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, directly into your command shell and see what it does (if the files still exist).

For these kinds of problems you can run CMake with the -debug-trycompile option and the test project won’t be automatically cleaned up.

The first message in the output was -- The C compiler identification is unknown which is pretty clear 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. Again this is pointing to a compiler installation problem.

Thank you. I’ll mark your answer as “Solution”. For more details, let us refer to https://stackoverflow.com/questions/59355908.

Hi, i got the same problem, but cmake found the compiler.

here is my CMakeError.log:

Compiling the C compiler identification source file "CMakeCCompilerId.c" failed.
Compiler: D:/c/mingw/bin/gcc.exe 
Build flags: 
Id flags:  

The output was:
1
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find crt2.o: No such file or directory
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmoldname
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmsvcrt
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmoldname
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmsvcrt
collect2.exe: error: ld returned 1 exit status


Compiling the CXX compiler identification source file "CMakeCXXCompilerId.cpp" failed.
Compiler: D:/c/mingw/bin/g++.exe 
Build flags: 
Id flags:  

The output was:
1
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find crt2.o: No such file or directory
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmoldname
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmsvcrt
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingw32
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmingwex
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmoldname
d:/c/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: cannot find -lmsvcrt
collect2.exe: error: ld returned 1 exit status


Detecting C compiler ABI info failed to compile with the following output:
Change Dir: D:/CmakeTest/build/CMakeFiles/CMakeTmp

Run Build Command(s):nmake /nologo cmTC_62407\fast && Le fichier spécifié est introuvable
Generator: execution of make failed. Make command was: nmake /nologo cmTC_62407\fast && 



Determining if the C compiler works failed with the following output:
Change Dir: D:/CmakeTest/build/CMakeFiles/CMakeTmp

Run Build Command(s):nmake /nologo cmTC_bcf19\fast && Le fichier spécifié est introuvable
Generator: execution of make failed. Make command was: nmake /nologo cmTC_bcf19\fast &&

the logs from the command:

-- Building for: NMake Makefiles
-- The C compiler identification is GNU 9.2.0
-- The CXX compiler identification is GNU 9.2.0
-- Detecting C compiler ABI info
CMake Error: Generator: execution of make failed. Make command was: nmake /nologo cmTC_62407\fast &&
-- Detecting C compiler ABI info - failed
-- Check for working C compiler: D:/c/mingw/bin/gcc.exe
CMake Error: Generator: execution of make failed. Make command was: nmake /nologo cmTC_bcf19\fast &&
-- Check for working C compiler: D:/c/mingw/bin/gcc.exe - broken
CMake Error at D:/c/cmake-3.19.0-win64-x64/share/cmake-3.19/Modules/CMakeTestCCompiler.cmake:66 (message):
  The C compiler

    "D:/c/mingw/bin/gcc.exe"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: D:/CmakeTest/build/CMakeFiles/CMakeTmp

    Run Build Command(s):nmake /nologo cmTC_bcf19\fast && Le fichier spécifié est introuvable
    Generator: execution of make failed. Make command was: nmake /nologo cmTC_bcf19\fast &&




  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:5 (project)


-- Configuring incomplete, errors occurred!
See also "D:/CmakeTest/build/CMakeFiles/CMakeOutput.log".
See also "D:/CmakeTest/build/CMakeFiles/CMakeError.log".