CLion 2022.2 Help

CMakeLists.txt

CMakeLists.txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both).

When you create a new project, CLion generates CMakeLists.txt file automatically and places it in the project root directory. To open a project, you can point CLion to the top-level CMakeLists.txt and choose Open as Project.

Example below shows the CMakeLists.txt file of a simple "Hello, World!" project:

cmake_minimum_required(VERSION 3.13) # CMake version check project(simple_example) # Create project "simple_example" set(CMAKE_CXX_STANDARD 14) # Enable c++14 standard # Add main.cpp file of project root directory as source file set(SOURCE_FILES main.cpp) # Add executable target with source files listed in SOURCE_FILES variable add_executable(simple_example ${SOURCE_FILES})

You can edit CMakeLists.txt files right in the Editor. Make sure to reload the project after editing. Automatic reload feature is disabled by default, you can turn it on by selecting the Automatically reload CMake project on editing checkbox in Settings / Preferences | Build, Execution, Deployment | CMake.

For projects with complex structure, you can create subdirectory CMakeList.txt files to describe the build, contents, and target rules for a subdirectory. Type of a target added by a subdirectory CMakeLists.txt file can differ depending on the role of the module.

cmakelists files in subdirectories

CLion provides code assistance in CMakeLists.txt files. Also, you can configure code style settings for this file format in Settings / Preferences | Editor | Code Style | CMake.

CMakeLists.txt file templates

When you create a CMake or CMake-based (CUDA, Qt) project via the New Project wizard, CLion uses different templates to generate CMakeLists.txt depending on the project type and settings. You can fine-tune these templates in Settings / Preferences | Editor | File and Code Templates, the Other tab:

File templates for new CMake projects

When editing a template text, you can use common predefined variables and the following CMake-specific variables:

${CMAKE_DEFAULT_PROJECT_FILE}

- the main.cpp/main.c/libary.cpp/library.c file of the project.

${CMAKE_LANGUAGE_VERSION}

- the selected language standard.

${CMAKE_MAJOR_VERSION}

- the major number of the minimum supported CMake version. For example, if the version if 3.20, this variable corresponds to 3.

${CMAKE_MINOR_VERSION}

- the minor number of the minimum supported CMake version. For example, if the version if 3.20, this variable corresponds to 20.

${CMAKE_LIBRARY_TYPE}

-SHARED for shared libraries.

${QT_VERSION}

- the selected Qt version.

${REQUIRED_LIBS}

- the Qt libraries required for the selected project type. By default, they are Core for Qt Console Executable and Core, Gui, Widgets for Qt Widgets Executable.

There is also a template for CMake scripts that you can add to an existing project via New | CMakeLists.txt in the context menu of the project view. By default, this template is empty. You can edit it using the same variables in Settings / Preferences | Editor | File and Code Templates, the Files tab:

File template for a new CMakeLists.txt file

Last modified: 12 February 2022