CLion 2020.2 Help

CMake profiles

Settings required for building a CMake project are incorporated in CMake profile. It includes toolchain, build type, CMake options such as CMake generators, and environment variables. You can have multiple profiles in order to, for example, use different compilers for one project, or to build CMake targets with differing settings.

Configure profiles

  • To set up the profiles for your project, go to Settings / Preferences | Build, Execution, Deployment | CMake.

  • To set a particular profile as default for all new projects, configure it in File | New Projects Settings | Settings for New Projects | Build, Execution, Deployment | CMake.

    Currently, this setting does not apply to the case of creating a CMake project from sources (CPP-17686).

Build with a profile

  • To build a target using a specific profile, select it in the Run/Debug configuration switcher on the toolbar (or press Alt+Shift+F10 for Run and Alt+Shift+F9 for Debug):

    cmake profiles in the configuration switcher

Compiler flags

In CLion, there are two ways to specify compiler flags: either in CMake options of a profile, or right in the CMakeLists.txt script.

Set compiler flags

  • Using CMake options

    Select the profile in Settings / Preferences | Build, Execution, Deployment | CMake and edit the CMake options field.

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

    Setting compiler flags in CMake options

  • In CMakeLists.txt

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

Generators

In the CMake options field, you can specify a CMake generator via -G. For example, in the case of Ninja:

specifying CMake generator

For Ninja Multi-Config, use -G "Ninja Multi-Config". Note that CLion will use only the configuration that corresponds to the build type of the current profile (see Current limitations).

You can set up any CMake generator available for your platform and use different generators in different CMake profiles if necessary.

When building your project, make sure to select the desired profile in the configuration switcher:

switch CMake profile

Note that for a CMake profile, the actual built tools are taken from the selected toolchain. If you specify the generator other than Makefiles, the Make path in the toolchain settings will be ignored.

Current limitations

  • CLion uses CMake File API, which first appeared in CMake v3.14. However, CLion supports it starting from the update introduced in CMake v3.15, so if you decide to switch from the bundled CMake, make sure to use version 3.15.x or later.

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

Build types

Use the Build type field to set one of the following CMake build types:

  • Default (corresponds to the empty value of CMAKE_BUILD_TYPE).

  • Debug (the default build type)

  • Release

  • RelWithDebInfo (Release with debugging information)

  • MinSizeRel (Release optimized for size)

To refer to the build type in CMakeLists.txt, use the CMAKE_BUILD_TYPE variable. For example:

if (CMAKE_BUILD_TYPE MATCHES Debug) add_definitions(-DDEBUG=1) endif()

You can also create conditional statements in your code based on the current build type:

int main(int argc, const char* argv[]) { #if DEBUG ... #else ... #endif

Custom build types

The list of the available build types is defined in the CMAKE_CONFIGURATION_TYPES command. The default value of this command is the four build types given above, but you can extend it to have other build types. For example:

# adding two custom build types to the cached value # variable CMAKE_CONFIGURATION_TYPES should be defined before other definitions: set(CMAKE_CONFIGURATION_TYPES "MyDebug;MyRelease" CACHE STRING "" FORCE)

After reloading the project, custom types will be available from the CMake settings:

custom build types

Note that the custom types were added to the value Debug which was cached in CMakeCache.txt. So for example, if you add a new CMake profile, it will have its own CMakeCache.txt, and for this profile, the list of the available build types will contain your custom types only:

custom build types for new cmake profile

Build options

In the Build options field, you can set the options to be passed either to the build tool used by CMake or as command line parameters to CMake itself. These options will be used during the build phase.

Arguments for the underlying build tool (make, Ninja, or another one) should be preceded with --. For example, if you specify -j 5 --clean-first -- -d -p, then -j 5 --clean-first will be processed by CMake, while -d -p will be passed to the build tool.

When nothing is specified in this field, CLion uses default settings which depend on the selected environment. For example, if the make generator is set, the default value of this field is -- -j <number_of_hardware_cores>, while for Microsoft Visual C++ this field is empty.

Environment variables

You can pass additional environment variables to CMake generation and build via the Environment field of the CMake Settings dialog (navigate to Settings / Preferences | Build, Execution, Deployment | CMake).

The overall impacting environment for CMake generation and build consists of:

  1. Parent environment

    To include parent environment, open the Environment Variables dialog by clicking icons.general.inlineVariables.svg or pressing Shift+Enter, and set the Include system environment variables checkbox. The values you specify additionally will be appended to system variables. Otherwise, when the checkbox is cleared, your custom values will overwrite the system ones.

  2. Toolchain environment

    For example, variables defined in vcvarsall.bat for MSVC, path variables like mingw/bin, and others.

  3. CMake profile environment

    Your custom variables specified in the Environment field.

Last modified: 27 November 2020