CLion 2017.1 Help

Changing Configuration Types

In this topic we are going to discuss the ways of optimizing the project generation depending on the purpose of a particular build.

Let's consider the debugging process. As a rule, debugging needs a lot of interstitial information, therefore you have to include a number of code fragments for logging, printing, etc. On the other hand, such code affects performance and increases the code size of your project.

To avoid such a conflict, CMake offers you an instrument known as a build configuration.

The following default build configuration types are provisioned by CMake:

  • Debug;
  • Release;
  • RelWithDebInfo;
  • MinSizeRel.

CLion lets you create as many build configuration as needed for your development lifecycle using the CMake Settings dialog and choosing the configuration among them that is optimal for the current purposes. For example, the following code sample will be executed only if the Debug build configuration had being selected:

int main(int argc, const char* argv[]) { #ifdef DEBUG printf("Executing as \"%s\"\n", argv[0]); #endif ...
and be ignored when another configuration type (Release, for instance) is used.

In this example, DEBUG must be defined in CMakeLists.txt file as follows:

add_definitions(DEBUG)

To apply the build configuration to the run/debug configuration, select it in the Configuration field of the respective Run/Debug configuration dialog or switch the corresponding resolve context.

Note: CMake is reloaded each time when you create a build configuration followed by generation of the CMake files. Therefore, creating of multiple build configurations gives you another big advantage: in this case you have to reload CMake only once upon changing the CMake settings. After that you have all the configurations and respective CMake files available without reload of CMake.

Advanced option: setting custom build configuration types

You can extend the list of available configuration types with custom ones by setting them explicitly in the CMakeLists.txt file.

To adjust the list of configuration types for a certain project, do the following:

  • in CMakeLists.txt file, specify CMAKE_CONFIGURATION_TYPES variable as in example below:
    cmake_minimum_required(VERSION 3.6) project(exampleProject) # variable CMAKE_CONFIGURATION_TYPES shall be defined prior to other definitions: set(CMAKE_CONFIGURATION_TYPES "CustomType1;CustomType2" CACHE STRING "" FORCE) # setting two custom # configuration types set(CMAKE_CXX_STANDARD 11) set(SOURCE_FILES main.cpp) add_executable(exampleProject ${SOURCE_FILES})

You can check the results in the CMakeCache text file by pressing the /help/img/idea/2017.1/icon_config.png button of the CMake tool window.

See Also

Last modified: 29 March 2017