Cmake Mingw makefile in windows

I am trying to create a build of MinGW Makefiles using cmake but I get the following error when I execute the command cmake -G "MinGW Makefiles" ..\source\ I have added mingw bin and cmake to my path. I get the following error on running the cmake -G commmand. I am trying to create a build of MinGW Makefiles using cmake but I get the following error when I execute the command cmake -G "MinGW Makefiles" ..\source\ I am trying to avoid adding the compiler to be used in the cmakelist.txt file as I saw that it is a bad practice. I searched and found alternative approaches using -D flag and saving the preferred settings in cache but I am curious to know why this one works in the tutorial but not on my system… I get the following error on running the cmake -G commmand

"C:/Program Files/Microsoft Visual Studio/2022/Community/VC/Tools/MSVC/14.33.31629/bin/Hostx64/x64/cl.exe"

is not able to compile a simple test program.

It fails with the following output:

Change Dir: C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt

Run Build Command(s):C:/MinGW/bin/mingw32-make.exe -f Makefile cmTC_29120/fast && C:/MinGW/bin/mingw32-make.exe  -f CMakeFiles\cmTC_29120.dir\build.make CMakeFiles/cmTC_29120.dir/build
mingw32-make.exe[1]: Entering directory 'C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt'
Building CXX object CMakeFiles/cmTC_29120.dir/testCXXCompiler.cxx.obj
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_cl_compile_depends --dep-file=CMakeFiles\cmTC_29120.dir\testCXXCompiler.cxx.obj.d --working-dir="C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt" --filter-prefix="Note: including file: " -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1433~1.316\bin\Hostx64\x64\cl.exe  /nologo /TP   /DWIN32 /D_WINDOWS /W3 /GR /EHsc  /MDd /Zi /Ob0 /Od /RTC1 /showIncludes /FoCMakeFiles\cmTC_29120.dir\testCXXCompiler.cxx.obj /FdCMakeFiles\cmTC_29120.dir/ /FS -c "C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt\testCXXCompiler.cxx"
testCXXCompiler.cxx
Linking CXX executable cmTC_29120.exe
"C:\Program Files\CMake\bin\cmake.exe" -E cmake_link_script CMakeFiles\cmTC_29120.dir\link.txt --verbose=1
"C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\cmTC_29120.dir --rc=rc --mt=CMAKE_MT-NOTFOUND --manifests -- C:\PROGRA~1\MICROS~4\2022\COMMUN~1\VC\Tools\MSVC\1433~1.316\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\cmTC_29120.dir\objects1  /out:cmTC_29120.exe /implib:cmTC_29120.lib /pdb:"C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build\CMakeFiles\CMakeScratch\TryCompile-mhmynt\cmTC_29120.pdb" /version:0.0 /machine:x64  /debug /INCREMENTAL /subsystem:console  kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib
RC Pass 1: command "rc /fo CMakeFiles\cmTC_29120.dir/manifest.res CMakeFiles\cmTC_29120.dir/manifest.rc" failed (exit code 0) with the following output:
The system cannot find the file specifiedCMakeFiles\cmTC_29120.dir\build.make:99: recipe for target 'cmTC_29120.exe' failed
mingw32-make.exe[1]: *** [cmTC_29120.exe] Error -1
mingw32-make.exe[1]: Leaving directory 'C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeScratch/TryCompile-mhmynt'
Makefile:126: recipe for target 'cmTC_29120/fast' failed
mingw32-make.exe: *** [cmTC_29120/fast] Error 2

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

-- Configuring incomplete, errors occurred! See also "C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeOutput.log". See also "C:/Users/gaura.YASH/OneDrive/Desktop/new_code/cmake_1st_proj/C-Make/1st project/build/CMakeFiles/CMakeError.log". PS C:\Users\gaura.YASH\OneDrive\Desktop\new_code\cmake_1st_proj\C-Make\1st project\build>

It looks like it is picking up a VS toolchain. Are you in a Visual Studio Developer command prompt? You should be in a MinGW prompt instead. Also note that a MinGW CMake build is required to build with MinGW; the stock Windows release targets the native platform.

I’m running into this issue too. CMake is detecting my installation of MSVC 2017 and then produces the same error seen by Tiwari. FWI: I’m building on Windows and opening my shell in VS Code. Do I need a different installation of CMake? I only see one installer for Windows x64. I’d really like to run my configuration step with just cmake -S . -B build -G "MinGW Makefiles".

I use MinGW regularly, even in msvc environment but I force the compiler on cmake command line, it may be the way to go (otherwise it actually seems that some autodetection kicks in).

Hope it helps

Thanks, that does help. I added -DCMAKE_C_COMPILER=gcc and it fixed the computer detection.