Setting Default Compiler in Visual Studio CMake - c++

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 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:
Open the Windows Start Search (by pressing the Windows Key), type
"env", and choose "Edit the system environment variables".
Click "Environment Variables...".
Under "System variables", click the "New..." button to add a new environment variable.
For "Variable name:", use CMAKE_GENERATOR, and for "Variable value:" use "MinGW
Makefiles".
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++"

Related

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?
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.
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
cmake uses Visual Studio generator for MinGW on Windows by default (even without Visual Studio!), this is the real annoying issue (Cygwin is not affected).
We need to work around MinGW only. What will be the most reliable hook for MinGW? I think MSYSTEM is very popular environment variable that will always be defined for MinGW.
You can place PreLoad.cmake in the project root with the following content:
if (NOT "$ENV{MSYSTEM}" STREQUAL "" AND "$ENV{VisualStudioVersion}" STREQUAL "")
find_program (CMAKE_NINJA_BINARY NAMES "ninja")
if (CMAKE_NINJA_BINARY)
set (
CMAKE_GENERATOR "Ninja"
CACHE INTERNAL "Cmake generator"
)
return ()
endif ()
find_program (CMAKE_MAKE_BINARY NAMES "gmake" "make")
if (CMAKE_MAKE_BINARY)
set (
CMAKE_GENERATOR "Unix Makefiles"
CACHE INTERNAL "Cmake generator"
)
return ()
endif ()
endif ()
Unfortunately this solution is not universal:
MSYSTEM=MINGW64 cmake -G "Unix Makefiles" ..
CMake Error: Error: generator : Unix Makefiles
Does not match the generator used previously: Ninja
Either remove the CMakeCache.txt file and CMakeFiles directory or choose a different binary directory.
If you are setting CMAKE_GENERATOR inside PreLoad.cmake than you are loosing ability to use -G cmake option. If you don't need this option, than this solution will be just fine.
PS It is not possible to access -G option value inside PreLoad.cmake as CMAKE_GENERATOR or another option. So it is not possible to add guard case like NOT DEFINED CMAKE_GENERATOR to check whether generator has been provided explicitly using -G option.

Modifying variables in cmake-gui

Here is said that to run cmake for each builds (debug and release) it is recommended to pass CMAKE_BUILD_TYPE as an argument to cmake, e.g. cmake -DCMAKE_BUILD_TYPE=Release .. or cmake -DCMAKE_BUILD_TYPE=Debug .. What is the equivalent of this in cmake-gui? When I press "Configure" then some variables pops-up in window and here I will create new one CMAKE_BUILD_TYPE=Debug and then press "Generate" is this equivalent of above mentioned command line passing?
Thanks
Yes. It is equivalent.
Note that CMAKE_BUILD_TYPE only works for single-configuration generators like Unix Makefiles. Generators like Visual Studio generate multi-configuration projects and the choice of the one being compiled is done in the IDE.
EDIT: As stated in comments, and unlike what I suggested below, CMAKE_BUILD_TYPE is not an advanced variable, and effectively needs to be added in the GUI.
Not sure, but CMAKE_BUILD_TYPE may already exists as an advanced option: check the "Advanced" checkbox in the GUI to show all the variables.

What is the default build configuration of cmake

In this answer, it says Debug is the default cmake build configuration.
But I have a different observation:
I have following in my CMakeLists.txt to choose debug and release versions of a lib according to the current build configuration.
target_link_libraries(MyApp debug Widgets_d)
target_link_libraries(MyApp optimized Widgets)
It seems that when I invoke cmake without sepcifying -DCMAKE_BUILD_TYPE flag, Widgets is used instead of Widgets_d (When I delete Widgets and try to build, make complains that lib is not there). So that means by default the build configuration is optimized, not debug.
So what actually is the default build configuration? If it is debug, what could be wrong with my CMakelists.txt?
target_link_libraries with optimized keyword corresponds to all configurations, which are not debug.
Try adding message(STATUS "Build type: ${CMAKE_BUILD_TYPE}") to your CMakeLists.txt to see the actual build type (I suppose it should be empty).
If depends on whether you are using a single-configuration generator (Makefiles) or a multi-configuration generator (Visual Studio, XCode).
The link cited in the question is about a multi-configuration generator. When using a multi-configuration generator, the configuration variable CMAKE_BUILD_TYPE is ignored. To select the configuration to build, cmake allows the switch --config, and this defaults to Debug. So
cmake --build .
in a multi-configuration project builds a Debug version.
However, when using a single-configuration generator, the switch --config is ignored. Only the configuration variable CMAKE_BUILD_TYPE is used to determine the build type, and this defaults to Release.
More background info on single- and multiconfiguration-generators in this answer.

Basic issue about Cmake

Im trying to use OpenCV but I keep on getting this error on Cmake:
CMake Error: CMake was unable to find a build program corresponding to "MinGW Makefiles"
I know what I have to do is : "cmake is having a hard time finding make and GCC, add the mingw bin dir to your PATH environment variable."
But I do not know:
how to add the mingw
where is the PATH environment variable?
This link describes how to add entries to the PATH environmental variable. You need to add the directory where your MinGW is installed there.
You can also consult wiki if you want some information what PATH is actually for.

Setting default compiler in CMake

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?
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.
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.
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.