简体   繁体   中英

Setting Default Compiler in Visual Studio CMake

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.

Perhaps, the easiest way to do this globally for all your new CMake projects is to set theCMAKE_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" ..

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++"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM