C++ – CMake & MinGW Compilation on Windows, without needing the -G “MinGW Makefiles” flag

ccmakemakefile

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?

Best Answer

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

You can't, not in any version of CMake released to date. CMake chooses a generator before it starts evaluating any CMakeLists.txt files. By default, it chooses a generator based on runtime platform and available toolsets, and command-line options are the only way presently available to influence or override CMake's choice of generator.

In comments, @Tsyvarev pointed out an open CMake issue report asking for the very same feature you are asking for. The associated comment thread provides more detail, and the last comment was earlier this year. I would guess that eventually CMake will add support for specifying a generator via environment variable, but for now, your -G option is the only available alternative. You could consider scripting it if you want to save keystrokes and reduce the risk of typos.

Related Topic