CMake Configuration Files in CLion


The settings required to build a CMake project are consolidated into a CMake configuration file. It includes the toolchain and build type, as well as CMake options such as generators and environment variables. You can configure multiple configuration files for a project to build targets with different compilers or different settings.
To set up a configuration file for a project, go to "Settings" | "Build, Execution, Deployment" | "CMake". You can quickly access this dialog from the Find Action (CtrlShift0A) (search CMake settings) or assign a shortcut key for it in "Settings" | "Editor" | "Keymaps".
CMake configuration files are configured for each project.

To add a new configuration file, go to "Settings" | "Build, Execution, Deployment" | "CMake".
Click "Add" and CLion will add a new configuration file to the list.
Change the configuration file name, build type, and other settings as needed.

The enabled configuration files will be listed in the Run/Debug Configurations switcher. Before building, running, or debugging the application, select the desired configuration file:

Click or invoke one of the available build actions. Additionally, by default, a build action will be executed before running the "Run Application" action or debugging (this setting can be changed in the configuration settings).

Default configuration file for new projects
  • Go to “File” | “New Project Settings” | “New Project Settings” | “Build, Execution, Deployment” | “CMake”.

  • Configure the list of configuration files that are to be used by default for all new projects.

Sharing Configuration Files

You can share CMake configuration files with the project into version control systems. The settings for configuration files are stored in the cmake.xml file under the .idea directory.

  • Select the configuration file to share and check the “Share” checkbox:

    CMake Configuration Files in CLion

    Note that only configuration file settings can be shared. For all configuration files, the option to reload the CMake project when editing CMakeLists.txt or other CMake configuration files is universal and stored in workspace.xml.

    Make sure to use different names for shared and local configuration files. If a shared configuration file and a local configuration file have the same name, the local configuration file will take precedence, and you will not see the shared configuration file in the settings.

Disable/Enable Configuration Files

You can disable currently unused configuration files to save loading time and avoid potential errors (for example, when you have a rarely used remote configuration file, and the machine is turned off).

When you enable or disable a configuration file, CLion will incrementally reload CMake, regardless of whether the option to control reloading CMake when editing CMakeLists.txt or other CMake configuration files is checked.

When reloading CMake, all configuration files will be reloaded in parallel. This may cause issues for certain configurations. To resolve this, select the option to “Reload CMake Configuration Files Sequentially” in “Settings” | “Advanced Settings” | “CMake”.

There are several options:

  • In “Settings” | “Build, Execution, Deployment” | “CMake”, clear or set the “Enable Configuration Files” checkbox. Disabled configuration files will appear gray in the list.

    CMake Configuration Files in CLion

  • To disable a successfully loaded configuration file, select “Disable This Configuration File” from the configuration menu in the CMake tool window:

    CMake Configuration Files in CLion

    From this menu, you can also enable any previously disabled configuration files:

    CMake Configuration Files in CLion

  • If a configuration file fails to load, you can disable it using the “Disable Configuration File” option in the CMake tool window:

    CMake Configuration Files in CLion

Compiler Flags

In CLion, you can specify compiler flags in the CMake options field of the configuration file or in the CMakeLists.txt script.

  • Using CMake options:

    In “Settings” | “Build, Execution, Deployment” | “CMake”, select the configuration file and edit the CMake options field.

    Use -D and the CMAKE_CXX_FLAGS variable (or CMAKE_C_FLAGS for C projects). For example, -DCMAKE_CXX_FLAGS=”-Wall -Wextra”.

    You can change the value of existing CMake variables in the cache variable table.

    CMake Configuration Files in CLion

    CLion can also use the –preset parameter passed to CMake options. Data from the specified preset will load into the build type, toolchain, and build directory settings.

  • In CMakeLists.txt:

    Alternatively, add the following line in your CMakeLists.txt script: set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -Wall -Wextra”)

CMake Toolchain File

  • In CMake options, you can specify the CMake toolchain file using the CMAKE_TOOLCHAIN_FILE variable.

CMake Cache Variables

CMake cache variables can be viewed and edited in the cache variable table:

You can add new variables in the CMake options field or CMakeLists.txt file. User-defined variables are at the top of the table. The values of user-defined variables are displayed in bold, while the changed values of other variables are displayed in italic:

CMake Configuration Files in CLion

To remove a variable from the CMake options field, select it in the table, then click the “Remove” button or press AltDelete. The variable will be removed from the table, or its value will be set to default after resetting the CMake cache.

CMake Configuration Files in CLion

To discard changes made in the current session, select the variable and click CMake Configuration Files in CLion or press Ctrl+Z.

Advanced CMake variables are hidden by default, but you can display them by selecting the “Show Advanced” checkbox:

CMake Configuration Files in CLion

CLion also displays a brief description in the tooltips of CMake cache variables:

CMake Configuration Files in CLion

The IDE displays boolean values as checkboxes. Next to the checkbox, you can see the actual value behind the boolean variable. The following values are considered False: 0, OFF, NO, N, IGNORE, NOTFOUND, empty string, and values ending with -NOTFOUND. In all other cases, the value is considered True.

When you check or uncheck a checkbox in the CLion user interface, the following value pairs are used to configure option values: 0-1, OFF-ON, NO-YES, FALSE-TRUE, and N-Y. For any other values, the IDE converts the checkbox state to OFF-ON.

Named boolean constants are case insensitive.

Generator

In the generator field, you can switch between CMake generators. Use the default settings of the selected toolchain or set other generators from the predefined list:

CMake Configuration Files in CLion

Use Default CLion uses Ninja or Makefiles as the default generator by default. This is used when the selected toolchain is local (excluding remote, Docker, and WSL); the selected toolchain is configured with build tools in Ninja (bundled Ninja is the default build tool); CMake version is 3.20 or higher; the project was first created or opened in CLion 2021.3; and the “Use traditional generator for CMake 3.20 and higher” checkbox in advanced settings is unchecked. In all other cases, the Makefiles generator will be used.
Let CMake Decide If this option is selected, CLion will not explicitly enforce any generator, and CMake will decide which generator to use. By default, this is controlled by the CMAKE_GENERATOR environment variable.

Alternatively, you can set the generator in the CMake options using -G. When using the generator field, CLion will automatically update the CMake options and vice versa:

CMake Configuration Files in CLion

The actual build tool is taken from the selected toolchain.

Current Limitations

  • CLion uses the CMake file API, which first appeared in CMake v3.14. However, CLion has supported it since CMake v3.15, so if you decide to switch to a version other than the bundled CMake, make sure to use version 3.15.x or higher.

  • For multi-configuration generators like Ninja Multi-Config, Xcode, or Visual Studio, CLion only uses configurations corresponding to the build type specified in the CMake configuration file (CPP-20890).

CMake Configuration Files in CLion


Recommended Articles
  • CMake Cache in CLion Tutorial

  • C Language Algorithm – “Combination” Algorithm Problem

  • C++ Tutorial – Detailed Explanation of static in C++ Language

Leave a Comment