0

I am using Visual Studio Community 2019. I always need to change the CMakeSettings.json for every new CMake Project I make.

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

How can i set MinGW as my default compiler so that i do not have to worry about setting compiler every time I create a new CMake Project.

There are solution given on this link:

Setting default compiler in CMake

but I am unable to follow any of them because they are not very clear for me.

Like the accepted solution says:

Set CMAKE_GENERATOR environment variable to specify the default generator to be used on your system.

But I don't know how to set CMAKE_GENERATOR environment variable to specify the default generator to be used on my system. I can do for my current project but i am unable to set the compiler at "C:/MinGW/bin/g++" as default for every new CMake Project. I know people have given working solutions but even after hours, due to very general instructions, i am unable to follow. Please provide step by step instructions with where to look for the file which i need to change.

1
  • 1
    CMAKE_GENERATOR="Mingw Makefiles" Commented Jan 13, 2020 at 14:22

2 Answers 2

3

Perhaps, the easiest way to do this globally for all your new CMake projects is to set the CMAKE_GENERATOR environment variable on your system (available with CMake 3.15 or greater). Since it appears you are using Windows, here is how to set it on Windows 10:

  1. Open the Windows Start Search (by pressing the Windows Key), type "env", and choose "Edit the system environment variables".
  2. Click "Environment Variables...".
  3. Under "System variables", click the "New..." button to add a new environment variable.
  4. For "Variable name:", use CMAKE_GENERATOR, and for "Variable value:" use "MinGW Makefiles".
  5. Click "OK", then "OK" again to save the new environment variable.

Now, CMake will use this environment variable to set MinGW Makesfiles as the default generator when new projects are invoked. You should also make sure the path to MinGW (C:/MinGW/bin/g++) is included in your Path environment variable.


If you are using an earlier version of CMake (< 3.15), you have to specify the generator manually when invoking CMake:

cmake -DCMAKE_GENERATOR="MinGW Makefiles" ..

or

cmake -G "MinGW Makefiles" ..
Sign up to request clarification or add additional context in comments.

13 Comments

Thanks for the response but unfortunately it doesn't seem to work for me. I set a new environment variable named exactly as "CMAKE_GENERATOR" and named the variable value exactly as "MinGW Makefiles". I already had this path "D:\MinGW\bin" in the path environment variable. It didn't work with it so i added this path "D:\MinGW\bin\g++" to the path environment variable. But it still didn't work. Can MinGW being D drive be a problem? Or can you think of something else.
@Pera What version of CMake are you using? To use it as an environment variable, you must have CMake 3.15 or greater. I expanded my answer to explain this.
Please bear with me on this. Can you tell how to check the version of CMake. I tried on my own but couldn't find it.. I found this line in CMakeLists.txt cmake_minimum_required (VERSION 3.8)
Sure! You can type cmake --version in your command prompt (if cmake executable is in your Path environment). If you installed CMake outside Visual Studio, you can also open the CMake GUI, which lists the version at the top of the GUI.
Ok, Visual Studio 2019 should ship CMake 3.13, which unfortunately, is not a sufficient version for the environment variable solution I proposed in my answer. You could manually specify the generator as suggested, or upgrade your CMake. It is easy to download and install.
|
0

But I don't know how to set CMAKE_GENERATOR environment variable to specify the default generator to be used on my system.

That variable is taken from the environment, but can also be sent as a parameter to CMake commands:

cmake .. -DCMAKE_GENERATOR="Mingw Makefiles"

In the command line you can also set the desired compiler:

cmake .. -DCMAKE_GENERATOR="Mingw Makefiles" -DCMAKE_CXX_COMPILER="C:/MinGW/bin/g++"

2 Comments

CMake will also read a system environment variable CMAKE_GENERATOR, if it is set.
I believe (from the documentation) you need CMake 3.15 or newer for this feature.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.