c++makefilecmake

CMake & MinGW Compilation on Windows, without needing the -G "MinGW Makefiles" flag


I want to build my C++ applications from the Windows PowerShell command line using CMake and MinGW.

When I do this in the "normal way," with these commands:

mkdir build
cd build
cmake ..
make

CMake chooses Visual Studio as the default compiler, and doesn't generate any Makefiles for me.

I want CMake to use MinGW as the default compiler, and generate Makefiles. It works exactly the way that I want it to when I run these commands, adding the -G "MinGW Makefiles" flag:

mkdir build
cd build
cmake .. -G "MinGW Makefiles"
make

How can I make CMake behave this way all the time, without adding the -G "MinGW Makefiles" flag?

I've tried setting up a CMAKE_GENERATOR environment variable in Windows, and pointing it to "path\to\mingw\bin", "path\to\mingw\bin\mingw32-make.exe", as well as a string that reads "MinGW Makefile".

None of these worked for me after running refreshenv and then trying to run cmake .. again.

Does anybody know if this is the correct environment variable to use in order to specify CMake's default behavior? If it is, what value should I be using?


Solution

  • How can I make CMake behave this way all the time, without adding the -G "MinGW Makefiles" flag?

    2024 Update: You CAN do this now.

    Starting from version 3.15 CMake supports the CMAKE_GENERATOR environment variable. You may set this environment variable to MinGW Makefiles in Windows Control Panel with steps described in this answer.

    If you still get the nmake error after setting the environment variables, you might want to remove your build directory and start a clean build.