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.
note
The source code of the sample project shown below is available on GitHub.
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
Select File | New Project from the main menu.
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.
Click Create.
CLion will generate a stub project with a single source file main.cpp and a CMakeLists.txt script under the root.
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.
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:
tip
The CMake tool window opens up automatically in case of a load failure.
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:
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:
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.
Right-click the root folder in the Project tree and select New | C/C++ Source File:
Set the Add to targets checkbox:
Click OK, and the new file will be added to the add_executable command:
tip
When you add a file to a target this way, without manually editing the CMakeLists.txt script, CLion reloads the project automatically in the background.
4. Adding new targets
Now let's add two more files and create an executable and a library target for them.
New executable target
Right-click the root folder in the Project tree and select New | C/C++ Source File again.
Set the file name (calc in our example).
In the Add to targets field, clear the cmake_testapp checkbox.
Click the Add new target link:
Specify the target name (cmake_testapp_calc) and click Add:
The newly created target will appear in the list.
Make sure that only the cmake_testapp_calc target is selected:
Click OK.
CLion will add a new add_executable command to CMakeLists.txt and reload the project:
After reload, a new configuration will appear in the configuration switcher:
note
This is an example to illustrate how to add a new executable target. Note that if you want to run this configuration, you need to add an int main() function to calc.cpp.
New library target
Right-click the root folder in the Project tree and select New | C/C++ Source File again.
Provide the file name (calc_lib).
Clear the checkboxes in the Add to targets field.
Click Add new target.
In the drop-down list, select add_library:
tip
In this drop-down, you can find the commands for adding CUDA and Qt targets.
Set the target name (cmake_testapp_lib) and click Add.
The newly created target will appear in the list.
Make sure that other targets' checkboxes are cleared.
Set the Create an associated header checkbox to also add a header file, calc_lib.h.
Click OK.
CLion will add a add_library command to CMakeLists.txt and reload the project:
Similarly to the case of executable targets, CLion automatically creates configurations for library targets:
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:
To obtain the library file, you need to build this configuration. Select it in the switcher and press CtrlF9 or click the build icon:
As the result, the libcmake_testapp_lib.a file will appear in the cmake-build-debug folder.
5. Reloading the project
Let's introduce some changes in CMakeLists.txt manually. For example, add STATIC to the declaration of the library target:
When we make changes in CMakeLists.txt, CLion shows the icon suggesting to reload the project. Click it or press the shortcut:
tip
If you prefer CLion to reload the project silently on every change in CMakeLists.txt, go to Settings | Build, Execution, Deployment | CMake and enable auto-reload:
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.
note
Find out more on using presets in CLion: CMake presets
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:
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:
tip
Switching configurations or CMake profiles may affect preprocessor definitions used for resolving your code. See resolve context for more information.
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.
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.
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".
9. Linking libraries
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.
Create a lib directory under the project root.
Copy libcmake_testapp_lib.a from its default location, which is cmake-build-debug, to the lib folder.
Add the following commands to link this library to the cmake_testapp target:
find_library provides the full path to the library,
which we then pass directly into the target_link_libraries command via the ${TEST_LIBRARY} variable.
note
Note: make sure to place target_link_libraries after the add_executable command, so that CMake actually builds the target before linking the 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.
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 Alt07 (for the tool window) or CtrlF12 (for the popup).
Code completion works for most of the elements in your CMakeLists.txt, including the arguments of commands like find_package():
Quick Documentation popup helps you get more information on code elements. To invoke it, use mouse hover or press Ctrl0Q.
You can view quick documentation even for completion suggestions:
You can adjust the color and font scheme for CMake files in Settings | Editor | Color Scheme | CMake:
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 a general description of the framework in CTest support.
Add CTest to the sample project
Create a ctest directory under the project root.
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.
In the main menu, go to Run | Edit Configurations.
Under the CTest Application node, you will find the automatically created All CTest configuration.
Since the code to be tested is located in the ctest_exe_addvalues_zero executable, we need to select that executable as a build target in the configuration settings:
tip
To apply this to all CTest configurations in the project, click Edit configuration templates... in the bottom-left corner of the same dialog, select CTest Application, and change the target in the template:
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:
Our website uses some cookies and records your IP address for the purposes of accessibility, security, and managing your access to the telecommunication network. You can disable data collection and cookies by changing your browser settings, but it may affect how this website functions. Learn more.
With your consent, JetBrains may also use cookies and your IP address to collect individual statistics and provide you with personalized offers and ads subject to the Privacy Notice and the Terms of Use. JetBrains may use third-party services for this purpose. You can adjust or withdraw your consent at any time by visiting the Opt-Out page.