CLion 2023.3 Help

Quick CMake tutorial

This tutorial will guide you through the process of creating and developing a simple CMake project in CLion. Step by step, you will learn the basics of CMake as a build system, along with CMake-specific IDE settings and actions.

1. Simple CMake project

CMake is a meta build system that uses scripts called CMakeLists to generate build files for a specific environment (for example, makefiles on Unix machines).

When you create a new CMake project in CLion, a CMakeLists.txt file is generated automatically under the project root.

Start a sample CMake project

  1. Select File | New Project from the main menu.

  2. Select C++ Executable on the left-hand pane.

    In our example, the project name is cmake_testapp and the selected language standard in C++17.

    Creating a new CMake project

    Click Create.

  3. CLion will generate a stub project with a single source file main.cpp and a CMakeLists.txt script under the root.

    CMakeLists.txt in a stub CMake project

    The automatically generated CMakeLists.txt contains the following commands:

    Command

    Description

    cmake_minimum_required(VERSION 3.26)

    Specifies the minimum required version of CMake, as set in the default toolchain. For most cases, if CMake executable was not changed intentionally, this is the bundled CMake version.

    project(cmake_testapp)

    Defines the project name according to what we provided during project creation.

    set(CMAKE_CXX_STANDARD 17)

    Sets the CMAKE_CXX_STANDARD variable to the value of 17, as we selected when creating the project.

    add_executable(cmake_testapp main.cpp)

    Adds the cmake_testapp executable target to be built from main.cpp. We'll get into targets further below.

  4. Click on the left-hand toolbar of the IDE to open the CMake tool window, where you can check the progress and status of project load:

    CMake tool window

2. CMake targets and CLion configurations

Target is an executable or a library to be built using a CMake script. You can define multiple build targets in a single script.

For now, our test project has only one build target, cmake_testapp. Upon the first project loading, CLion automatically adds a run/debug configuration associated with this target:

Default configuration for a new CMake project

In the switcher, click Edit Configurations to view the details. The target name and the executable name are the same as specified in CMakeLists.txt:

Details of the default configuration for a new CMake project

Notice the Before launch area of this dialog. Build is set as a before launch step by default. So we can use this configuration not only to debug or run our target, but also to perform the build. For more information about various build actions available in CLion, check out Build actions.

3. Adding files to existing targets

Let’s create a new source file general.cpp and add it to the cmake_testapp target.

  1. Right-click the root folder in the Project tree and select New | C/C++ Source File:

    Adding a source file
  2. Set the Add to targets checkbox:

    Adding a file to an existing target
  3. Click OK, and the new file will be added to the add_executable command:

    Source file added to add_executable

4. Adding new targets

Now let's add two more files and create an executable and a library target for them.

New executable target

  1. Right-click the root folder in the Project tree and select New | C/C++ Source File again.

  2. Set the file name (calc in our example).

  3. In the Add to targets field, clear the cmake_testapp checkbox.

  4. Click the Add new target link:

    Adding a file to a new target
  5. Specify the target name (cmake_testapp_calc) and click Add:

    Adding a new executable target
  6. The newly created target will appear in the list.

    Make sure that only the cmake_testapp_calc target is selected:

    New target added to the list of targets

    Click OK.

  7. CLion will add a new add_executable command to CMakeLists.txt and reload the project:

    New target in CMakeLists.txt
  8. After reload, a new configuration will appear in the configuration switcher:

    Configuration for the newly added target

New library target

  1. Right-click the root folder in the Project tree and select New | C/C++ Source File again.

  2. Provide the file name (calc_lib).

  3. Clear the checkboxes in the Add to targets field.

  4. Click Add new target.

    Adding new target
  5. In the drop-down list, select add_library:

    New library target
  6. Set the target name (cmake_testapp_lib) and click Add.

    Adding a library target
  7. The newly created target will appear in the list.

    Make sure that other targets' checkboxes are cleared.

    New library target added

    Set the Create an associated header checkbox to also add a header file, calc_lib.h.

    Click OK.

  8. CLion will add a add_library command to CMakeLists.txt and reload the project:

    New library target in CMakeLists.txt
  9. Similarly to the case of executable targets, CLion automatically creates configurations for library targets:

    Configuration for the newly added library target

    However, this is a non-executable configuration, so if you try to run or debug it, you will get the Executable not specified error message:

    Executable not specified error
  10. To obtain the library file, you need to build this configuration. Select it in the switcher and press Ctrl+F9 or click the build icon:

    Building a library target

    As the result, the libcmake_testapp_lib.a file will appear in the cmake-build-debug folder.

    Library artifact

5. Reloading the project

Let's introduce some changes in CMakeLists.txt manually. For example, add STATIC to the declaration of the library target:

Manually changing CMakeLists.txt

When we make changes in CMakeLists.txt, CLion shows the icon suggesting to reload the project. Click it or press the shortcut:

CMake reload icon

6. CMake presets and CLion CMake profiles

To configure and share CMake options for your project, you can use CMake presets, CLion's CMake profiles, or both.

CMake Profiles have many settings in common with CMake Presets and are also shareable via VCS. The major difference is that profiles reference CLion toolchains, which contain information that is not present and not needed in CMake presets (like the debugger or environment settings).

CMake presets

CMake Presets are a way to configure and share CMake options using two files:

  • CMakePresets.json for project-wise builds. This file can be shared via VCS.

  • CMakeUserPresets.json for developers' own local builds. This file should not be checked into VCS.

Both of these files have the same format and should be located in the project's root directory.

    CMake profiles

    A profile includes toolchain and build type, as well as CMake options such as generators and environment variables. You can configure multiple profiles for your project in order to, for example, use different compilers or to build targets with differing settings.

    See the next chapter for an example of adding a profile for Release build.

      7. Build types

      All the Run/Debug configurations created so far were Debug configurations, which is the default build type of the CMake profile that was automatically configured for our project.

      For example, let's separate the Debug and Release builds. For this, add () a new CMake profile in Settings | Build, Execution, Deployment | CMake and set its build type to Release:

      Adding a release CMake profile

      Notice the Build directory field that specifies the location of build results. The default folders are cmake-build-debug for Debug profiles and cmake-build-release for Release profiles. You can always set other locations of your choice.

      Save the profile, and it will appear in the switcher:

      CMake profiles in the configuration switcher

      8. Adding include directories

      To use additional headers located in separate directories, you need to add them either to all targets or to some specific ones.

      1. As an example, let's create three directories under the project root: includes, includes/general, and includes/math. Use the New | Directory option in the project tree context menu.

        New include directories
      2. Open CMakeLists.txt and add the following commands:

        target_include_directories (cmake_testapp_lib PUBLIC includes/math) target_include_directories (cmake_testapp_lib PUBLIC includes/general)

        These two commands make the headers located in general and math available from the sources of the cmake_testapp_lib target.

        For example, if we place a header called header_math.h inside the includes/math folder, we can then include it from calc_lib.cpp using #include "header_math.h".

      Static libraries

      On step 4, we created a static library called cmake_testapp_lib (the default filename is libcmake_testapp_lib.a).

      Now let's see how this library can be linked to our project. For convenience, we will create and use a separate folder for it.

      1. Create a lib directory under the project root.

      2. Copy libcmake_testapp_lib.a from its default location, which is cmake-build-debug, to the lib folder.

        Library artifact in the lib folder
      3. Add the following commands to link this library to the cmake_testapp target:

        find_library(TEST_LIBRARY cmake_testapp_lib lib) target_link_libraries(cmake_testapp LINK_PUBLIC ${TEST_LIBRARY})
        Linking a static library

      Dynamic libraries

      To illustrate the case of shared (dynamic) libraries, we will take the example of using LibPNG, a C library for reading and writing Portable Network Graphics (PNG) files.

      Install the library package using vcpkg

      CLion integrates with vcpkg, a package manager for C/C++. We will use it to quickly install the library right from the IDE.

      1. Follow this instruction to install vcpkg.

      2. In the vcpkg tool window, search for libpng:

        Search for libpng in vcpkg
      3. Click Install in the right-hand pane and wait for installation to complete.

      Add the library to CMakeLists.txt

      1. Open the CMakeLists.txt file and add the following commands:

        find_package(PNG REQUIRED) include_directories(${PNG_INCLUDE_DIR}) target_link_libraries(cmake_testapp PRIVATE ${PNG_LIBRARY})
      2. To check whether the library is linked correctly, try including one of the library headers to main.cpp:

        Including a LibPNG header

        For example, include png.h. You can then press Ctrl+B to to open this file:

        Go to the png.h file

      10. CMake debug

      In case there are errors or unwanted behaviour during CMake configuration, you can debug the CMake script similarly to other code in your project.

      1. Place breakpoints in your CMakeLists.txt file or files.

      2. Open the top-level CMakeLists.txt, click the gutter icon next to the first command, and select Debug:

        Starting a CMake debug session from gutter
      3. CLion will start a CMake debug session:

        CMake debug session

        For further information, refer to CMake debug features.

      11. Tips on editing CMakeLists.txt

      CLion provides code insight features to help you work with CMake scripts effectively. For example:

      • Structure view for CMake shows variables, functions, macros, and targets used your script. To open it, press Alt+7 (for the tool window) or Ctrl+F12 (for the popup).

        CMake script structure
      • Code completion works for most of the elements in your CMakeLists.txt, including the arguments of commands like find_package():

        Completion for find_package
      • Quick Documentation popup helps you get more information on code elements. To invoke it, use mouse hover or press Ctrl+Q.

        You can view quick documentation even for completion suggestions:

        Quick documentation for completion suggestions
      • You can adjust the color and font scheme for CMake files in Settings | Editor | Color Scheme | CMake:

        Color scheme settings for CMake

      12. Working with CTest

      This chapter gives a simple example of how to use CTest, a framework for compiling and running tests as part of the CMake build process. You can find general description of the framework in CTest support.

      Add CTest to the sample project

      1. Create a ctest directory under the project root.

      2. Add the following files to the ctest directory:

        • A source file addvalues_zero.cpp. Don't link this file with any CMake target for now.

        • A header file assert_macro.h. Don't link this file with any CMake target for now.

        • A CMakeLists.txt script.

        Adding a folder for CTest
      3. Add the following lines to ctest/CMakeLists.txt:

        cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR) add_executable(ctest_exe_addvalues_zero addvalues_zero.cpp) add_test(ctest_addvalues_zero ctest_exe_addvalues_zero)

        The first line states the minimum supported version of CTest, which corresponds to the version of CMake, 3.14.

        We are using the add_test command here to register the ctest_exe_addvalues_zero executable with CTest.

      4. Now we can place the actual code inside the test sources:

        • assert_macro.h:

          #include <iostream> #include <sstream> #define assertEqual( ... ) \ do { \ if( !( __VA_ARGS__ ) ) { \ std::cerr << "Unit test assert [ " \ << ( #__VA_ARGS__ ) \ << " ] failed in line [ " \ << __LINE__ \ << " ] file [ " \ << __FILE__ << " ]" \ << std::endl; \ err_code = 1; \ } \ } while( false )
        • addvalues_zero.cpp:

          #include "assert_macro.h" int test_addvalues_zero() { int err_code = 0; assertEqual (0+0 == 0); assertEqual ((-5)+5 == 0); return err_code; } int main() { return test_addvalues_zero(); }
      5. Next, we need to enable CTest and declare the subproject in the top-level CMakeLists.txt:

        enable_testing() add_subdirectory(ctest)

        The enable_testing command creates a built-in target test which will execute CTest.

      6. Reload the project.

      Add the code to be tested

      Let's prepare a code piece to be tested with CTest.

      1. In calc_lib.cpp, add a simple function

        int add_values (int a, int b) { return a+b;}
      2. In the associated header, calc_lib.h, add the function declaration:

        int add_values (int a, int b);
      Adding a function to be tested

      Edit the CTest configuration

      1. In the main menu, go to Run | Edit Configurations.

      2. Under the CTest Application node, you will find the automatically created All CTest configuration.

        Since the functions to be tested are located in the cmake_testapp_lib library, we need to change Target to build in the configuration settings:

        CTest configuration - changing the target
      3. Let's also check the Test list to run field. Click the pen icon to open the List of Available Tests dialog, where we can view and adjust the set of tests:

        List of the available tests in CTest configuration

      Run the CTest configuration and explore the results

      1. Select All CTest in the configuration switcher and click The Run button or press Shift+F10.

        Running a CTest configuration
      2. Find results in the Test Runner window:

        CTest results

      13. Useful links

      To dig deeper into CMake in CLion, learn how to:

      Last modified: 11 February 2024