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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
Pera
  • 173
  • 2
  • 14

2 Answers2

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" ..
Kevin
  • 16,549
  • 8
  • 60
  • 74
  • 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 Jan 13 '20 at 16:08
  • @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. – Kevin Jan 13 '20 at 16:11
  • 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)` – Pera Jan 13 '20 at 16:19
  • 1
    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. – Kevin Jan 13 '20 at 16:23
  • yes i think we got the problem probably. when i type `cmake --version` in cmd, it says `cmake` "is not recognised as ......". i think cmake is not set to path. But i didn't install cmake outside visual studio. i installed cmake from visual studio installer itself. but its not set to path. i don't know it's location so i can't set it to path. If i need to set the path then can you tell where it is installed by default or i am wrong in this and I need to fix something else... – Pera Jan 13 '20 at 16:27
  • 1
    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](https://cmake.org/download/) and install. – Kevin Jan 13 '20 at 16:36
  • ***cmake --version in cmd, it says cmake "is not recognised as ......".*** That means that your `PATH` environment variable does not have the folder containing the cmake executables. You can fix that similar to how you added the `CMAKE_GENERATOR` environment variable. However as @squareskittles said you likely need a newer version of CMake anyways. – drescherjm Jan 13 '20 at 18:08
  • Hey, I finally got the cmake directory inside the visual studio folder. I included the directory to path. Now cmake command is working and the version is "3.15.19101501-MSVC_2". Still it doesn't work. I think the problem is in this instruction : `You should also make sure the path to MinGW (D:/MinGW/bin/g++) is included in your Path environment variable.` I have this path set `D:/MinGW/bin` and inside the bin directory i have g++ executable. When I create a new project, while building it sets the CXX compiler to MSVC only. What's going wrong? – Pera Jan 14 '20 at 14:23
  • This command `cmake -DCMAKE_GENERATOR="MinGW Makefiles" ..` gives the following error _The source directory "C:/Users" does not appear to contain CMakeLists.txt. Specify --help for usage, or press the help button on the CMake GUI._ – Pera Jan 14 '20 at 14:25
  • I assume your `CMakeLists.txt` is not in `c:\Users`.. You probably have a space in your path and need to quote it. – drescherjm Jan 14 '20 at 14:35
  • ***I have this path set D:/MinGW/bin and inside the bin directory i have g++ executable. When I create a new project, while building it sets the CXX compiler to MSVC only. What's going wrong?*** Open a cmd.exe and type `g++` if it says ***g++ is not recognized as an internal or external command,*** your path is not setup correctly – drescherjm Jan 14 '20 at 14:39
  • g++ command works fine. its not "C:/Users" now. it went to cmake/bin directory in cmd and now this is the error `CMake Error: The source directory "D:/Microsoft Visual Studio/2019/Community/Common7/IDE/CommonExtensions/Microsoft/CMake/CMake" does not appear to contain CMakeLists.txt.` for `cmake -G "MinGW Makefiles" ..` – Pera Jan 14 '20 at 14:43
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/205948/discussion-between-pera-and-drescherjm). – Pera Jan 14 '20 at 14:45
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++"
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141