CLion 2019.3 Help

CMake Profile

In CLion, settings required for building a CMake project are incorporated in a CMake profile, which includes toolchain, build type, CMake options such as CMake generators, and environment variables. Having multiple profiles enables you, for example, to use different compilers for one project, or to build CMake targets with differing settings.

To configure CMake profiles for your projects, go to Settings / Preferences | Build, Execution, Deployment | CMake.

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

cmake profiles in the configuration switcher

Generators

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

specifying CMake generator

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 issues and 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.

  • Currently, recompiling individual files is not supported for non-default generators (CPP-17622).

  • For multi-configuration generators like Xcode and Visual Studio, CLion will use only the configuration that corresponds to the build type specified in the CMake profile.

Build types

By default, CMake supports the following build types:

  • Debug

  • Release

  • RelWithDebInfo (Release with debugging information)

  • MinSizeRel (Release optimized for size)

You can select the required build type in Settings / Preferences | Build, Execution, Deployment | CMake. To refer to the build type, use CMAKE_BUILD_TYPE. For example, add the following code into the CMakeLists.txt script:

if (CMAKE_BUILD_TYPE MATCHES Debug) add_definitions(-DDEBUG=1) endif()
And you will be able to create conditional statements in your code based on the current build type:
int main(int argc, const char* argv[]) { //TODO: add some code #if DEBUG ... #else ... #endif

Сustom 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

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 effective environment for CMake generation and build consists of:

  1. Parent environment

    To include parent environment, open the Environment Variables dialog by clicking icons actions menu open 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: 6 December 2019