51

I'm using CMake version 2.8 on WinXP SP3. Whenever i run my CMakeLists script by default CMake use Visual Studio 10 compiler. I've tried to do:

SET( CMAKE_CXX_COMPILER "C:/MinGW/bin/g++" )

without success. How can i set MinGW as my default compiler so that i do not have to worry about setting compiler in the CMakeLists?

2

3 Answers 3

34

CMake 3.15 or later supports overriding the default generator by setting the environment variable CMAKE_GENERATOR.

E.g., using PowerShell, set the environment variable in the following way to make MinGW the default generator:

$Env:CMAKE_GENERATOR = 'MinGW Makefiles'

For older CMake versions (< 3.15), CMake uses the newest Visual Studio installation as default generator, unless the generator is explicitly specified upon invoking CMake. This behavior is hard coded and cannot be changed.

As a work-around you can use a batch wrapper script titled cmake.cmd with the following contents:

@cmake.exe -G "MinGW Makefiles" %*

The script should be placed in a directory on the system PATH and should take precedence over the CMake executable cmake.exe.

The script invokes cmake.exe with MinGW as a generator and forwards all other parameters to it.

23

You only have to set the toolchain/output format once, typically you'd do this upon running cmake for the first time:

cmake -G "MinGW Makefiles" .

Instead of the dot you can use your own parameters (if any) and/or the path to the source.

As an alternative, especially when you're new to CMake, use the GUI version under windows (run cmake-gui without parameters instead of cmake).

Once opened, set your paths and click on "Configure". If there's no compiler set, it will ask you to pick one (otherwise you have to clear the cache to make it reappear).

Updated configuration values will appear in red and it will also allow you to select files and paths using the common Windows dialog boxes.

Once configuration is complete and without errors you can hit "generate" to create your makefiles or project files. To update these later on, you can use cmake-gui again or just use the usual command line version cmake.

2
  • 2
    A note about using cmake -G "MinGW Makefiles" . At least for me I had to make sure that MinGW\bin directory appears in the Path after VisualStudio\VC\bin directory. Before that even with the MinGW generator the c1.exe compiler was used instead of g++ from GNU
    – odedsh
    Commented Oct 30, 2013 at 9:11
  • That doesn't set the compiler though, only the generator.
    – burito
    Commented Jul 11, 2018 at 15:09
4

With CMake version 3.15 or later, you can set the CMAKE_GENERATOR environment variable to specify the default generator to be used on your system.

2
  • 1
    The question was about the compiler, not the cmake generator...
    – Nikos C.
    Commented Oct 13, 2020 at 13:23
  • 1
    @NikosC. No, it was not, even though the headline seems to indicate so. In the original problem, the configure run defaulted to the Visual Studio 10 generator, which has no support for compiling with g++. The only way to solve the problem in that scenario is by switching generators. Commented Oct 13, 2020 at 19:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.