CLion 2018.2 Help

Configuring CMake

CMake profile

In CLion, all the settings required for building a project are incorporated in a CMake profile. It accumulates toolchains, build types, CMake options, and environment variables (in other words, the compiler, linker, and CMake settings). You can create multiple profiles with different build types and toolchains:

Cmake Profile

This approach enables you, for example, to use different compilers for one project, or to build CMake targets with differing settings.

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

cl profileSwitcher

Changing build configuration types

By default, CMake supports the following build types:

  • Debug,
  • Release,
  • RelWithDebInfo,
  • MinSizeRel.

You can select the required build type in the CMake settings dialog.

In CLion, you can create as many build types as needed for your development lifecycle. Use the CMake Settings dialog and choose the configuration that is optimal for the current purposes. For example, the following code sample will be executed only if the Debug build type is selected:

int main(int argc, const char* argv[]) { #if DEBUG printf("Executing as \"%s\"\n", argv[0]); #endif ...
For another configuration type (such as Release), this code will be ignored.

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

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

Loading and unloading a CMake project

You may face the following cases when working on your project:

CMakeLists.txt file is not detected

If you open a project in CLion, but no CMakeLists.txt file is detected (e.g. the project does not contain a CMakeLists.txt file or it is located out of the project root folder), you can see that there is no CMake tool window opened. Besides, the CMake related options such as Reload CMake Project, Change Project Root Directory, and other options of the main and context menus are unavailable: the project is not associated with the CMake build system. To associate your project with CMake, click the warning message Select CMakeLists.txt file:

cl SelectCMake
Select the desired CMakeLists.txt in the dialog that opens.

CLion detected an improper CMakeLists.txt file

If CLion detects a CMakeLists.txt file, but you want to use another one for your project, then you need to disassociate a project from CMake, and then link the project again with the proper CMakeLists.txt. To do that:

  1. Go to the Tools | CMake main menu option and select Unload CMake Project option from the drop down menu:

    cl SelectUnloadCmake

  2. Upon this action, the CLion's state is as described in case #1. Now do one of the following:

    • Click Select CMakeLists.txt file and select the proper CMakeLists.txt file from the dialog.
    • Find the desired CMakeLists.txt file in the Project View and right click it. Select Load CMake Project from the context menu.

Unload CMake

If you need to unlink your project off the CMake build system completely, use the Unload CMake Project action as described above.

Compiler settings

In addition to setting a toolchain, you can use CMake options to specify a compiler. Go to Settings / Preferences | Build, Execution, Deployment | CMake on the main menu and specify the desired compiler by passing the following command:

-D CMAKE_<LANG>_COMPILER=[fully qualified compiler name]
The LANG part specifies the language to compile (C for C and CXX for C++), and you need to provide the full path to the compiler, for example:
Note: if you specify a compiler via CMake settings, you override the selection made in the toolchains setting dialog.

Environment variables

You can pass additional environment variables to CMake generation and build. To set the variables, use the Environment field of the Settings / Preferences | Build, Execution, Deployment | CMake dialog. In this case, the overall effective environment for generation and build will consist of:

  • The parent environment (in case the checkbox Include parent environment variables is set). Click the Show link to view the full list of system variables and their values.
  • The toolchain environment (for example, variables defined in vcvarsall.bat for MSVC, path variables like mingw/bin, and others).
  • The CMake profile environment (variables specified in CMake Settings overwrite those set in the parent and toolchain environments).

Advanced options

Changing build directory

The CMake settings dialog provides an input box for specifying the build options, and also enables you to change the CMake files generation path. CLion supports in-source builds: you can specify a directory for generated files within the source folder.

Creating multiple toolchains

You may want to have an individual set of tools ready to be used in different projects (for example, if your projects require different environments or CMake executables). Or you may need different toolchains to be used in one project for different CMake profiles (see the detailed description of build configurations).

In CLion, you can create and manage the list of toolchains and use them in CMake profiles. For the latter, use the Toolchains field of the CMake Settings dialog to select the desired toolchain from the drop-down list.

Setting custom build types

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

To adjust the list of build types, specify the CMAKE_CONFIGURATION_TYPES variable, for example::

cmake_minimum_required(VERSION 3.6) project(exampleProject) # setting two custom build types # variable CMAKE_CONFIGURATION_TYPES shall be defined prior to other definitions: set(CMAKE_CONFIGURATION_TYPES "CustomType1;CustomType2" CACHE STRING "" FORCE) set(CMAKE_CXX_STANDARD 11) # setting the language standard set(SOURCE_FILES main.cpp) add_executable(exampleProject ${SOURCE_FILES})

To check the results in the CMakeCache text file, press icons fileTypes config in CMake Tool Window.

Last modified: 16 June 2018