Modifying variables in cmake-gui - c++

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.

Related

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

Changing current cmake generator

How can I change current cmake generator without using Cmake GUI (I want to change standart generator to Visual Studio 12 2013 Win64)?
For those seeking the CMake GUI answer.
Using the GUI menu
Go to File->Delete Cache and then click Configure again.
A scenario where changing the generator is needed is that you are keeping the CMake GUI open and reusing the same directory (source and CMakeList.txt) to do simple examples or tutorials and deleting the build files before starting again, e.g. with out-of-source build it would be the entire build directory. This is not the standard use case for CMake but one that would be common among beginners.
You cannot reliably change the generator used for an output (= binary) directory once generation has already happened there once. You should start in a fresh output directory.
Once there, simply use CMake's -G command-line option:
cmake -G "Visual Studio 12 2013 Win64"

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.

Eclipse indexer proper c++11 syntax highlighting when generating projects with cmake

I know you can enable proper syntax highlighting with the GXX_EXPERIMENTAL hack described here:
Eclipse CDT indexer does not know C++11 containers
But i think, when generating projects with cmake, one should never need to touch the project settings at all.
So. Is there a simpler solution?
The answer is pretty simple.
The eclipse cdt generator ignores the definitions added with add_definitions(...) when parsing the symbols. Instead it uses the CMAKE_CXX_COMPILER_ARG1.
So all you have to do is: Add -DCMAKE_CXX_COMPILER_ARG1=-std=c++11 when invoking cmake
Generating project files from commandline:
cmake ../../src -G"Eclipse CDT4 - MinGW Makefiles" -DCMAKE_ECLIPSE_VERSION=4.2 -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_ARG1=-std=c++11
Generating projects from cmake gui:
- Select source and build directory.
- now BEFORE hitting configure or generate. Press "Add Entry" and add a new entry. Name:CMAKE_CXX_COMPILER_ARG1 Type:STRING Value:-std=c++11
- press Generate and create the Eclipse project
It is important to set the CMAKE_CXX_COMPILER_ARG1 upfront before hitting configure or generate the first time!
That´s it. The project will be generated with correct symbols. indexer, syntax highlighting and autocompletion should work as intended without changing any project settings by hand.
As already mentioned, the invocation of the project generators runs before the parsing of the CMakeLists.txt.
Thus, any definitions inside of CMakeLists.txt don't have any effect on the generated project files.
In case of eclipse-project generation, the capabilities of the compiler are requested inside CMakeExtraGeneratorDetermineCompilerMacrosAndIncludeDirs.cmake.
In line 23 the variable CMAKE_CXX_FLAGS is parsed which is evaluated in line 30.
Especially this CMAKE_CXX_FLAGS variable can only be set while invoking cmake from command line.
Recommodation of proper cmake invokation: cmake -G"Eclipse CDT4 - Unix Makefiles" -DCMAKE_CXX_FLAGS="-std=c++11" (or replace c++11 with eg. c++14 or any other standard you like)
Hint:
The result from evaluating line 30 can be seen from running the following command as an example: touch /tmp/dummy; /usr/bin/c++ -v -E -x c++ -std=c++11 -dD /tmp/dummy. It outputs all the defines from the compiler which are parsed into the eclipse-project:
...
#define __STDC__ 1
#define __cplusplus 201103L
...
In version 3.1 of cmake a new variable CMAKE_CXX_STANDARD was introduced that can activate C++11 support. Try something like this:
cmake ../../src -G "Eclipse CDT4 - Unix Makefiles" -DCMAKE_CXX_STANDARD=11
This seems to carry through to the generated Eclipse project. I tried it with verison 3.2.0-rc2 and Eclipse recognized C++11 features like std::shared_ptr<>.
Some documentation links:
CMAKE_CXX_STANDARD
Optional Compile Features (read towards the bottom of the section)

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.