Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 595
Soren Richenberg Avatar asked Nov 28 '19 20:11

Soren Richenberg


2 Answers

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.

like image 99
John Bollinger Avatar answered Oct 13 '22 01:10

John Bollinger


I know this is late. but in case someone came here and find this answer useful
all you have to do is to create a function something like this:

function gcmake {cmake .. -G "MinGW Makefiles"}

then you can simply type

mkdir build
cd build
gcmake ..
make

tip: you can add this function to your profile so that it will be saved to any new session. you can follow this nice guide

like image 39
mody Avatar answered Oct 13 '22 00:10

mody