4

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

Joachim W
  • 7,290
  • 5
  • 31
  • 59
  • After the phrase "is not able to compile a simple test program." it should be additional output which contains the error message caused by the compiler or a linker.. – Tsyvarev Dec 16 '19 at 12:22
  • Yes, but that output just tells us that the compiler fails to compile `...\CMakeTmp\testCCompiler.c`. It further points to `CMakeOutput.log` and `CMakeError.log`, which also say nothing about the cause of the failure. – Joachim W Dec 16 '19 at 12:26
  • The question https://stackoverflow.com/questions/29883383/clion-errorthe-c-compiler-c-mingw-bin-gcc-exe-is-not-able-to-compile-a-simp referred by you contains that additional information. – Tsyvarev Dec 16 '19 at 13:25
  • Sorry, what information? – Joachim W Dec 16 '19 at 13:31
  • I mean the rest part of the output, from "It fails with the following output:" till "CMake will not be able to correctly generate this project.". Please, do not *describe* the error messages (e.g. "From the following lines we learn that..."), instead **show** them as is. See [that question](https://meta.stackoverflow.com/questions/359146/why-should-i-post-complete-errors-why-isnt-the-message-itself-enough) on Meta. – Tsyvarev Dec 16 '19 at 13:40
  • ok, I pasted the full error message – Joachim W Dec 16 '19 at 13:51

2 Answers2

2

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.
Joachim W
  • 7,290
  • 5
  • 31
  • 59
1

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.

fdk1342
  • 3,274
  • 1
  • 16
  • 17
  • So we agree that the root of the problem is the incorrect gcc installation from the MinGW Chocolatey package, right? In the meantime, I reported this on the Chocolatey MinGW Disqus page. – Joachim W Dec 17 '19 at 12:56
  • Yes, it seems that it couldn't compile a simple file. – fdk1342 Dec 17 '19 at 13:04
  • I had to restart the pc after reinstalling mingw – gl3yn Jul 20 '23 at 09:39