CMakeLists.txt file contains a set of instructions and descriptions that define the source files and targets (executable files, libraries, or both) of a project.
When you create a new project, CLion automatically generates a CMakeLists.txt file and places it in the project root directory. To open the project, you can point CLion to the top-level CMakeLists.txt and select "Open as Project".
The following example shows a simple "Hello, World!" project's CMakeLists.txt file:
cmake_minimum_required(VERSION 3.13) # Check CMake version
project(simple_example) # Create project "simple_example"
set(CMAKE_CXX_STANDARD 14) # Enable C++14 standard
# Add the main.cpp file in the project root as a source file
set(SOURCE_FILES main.cpp)
# Add an executable target using the source files listed in the SOURCE_FILES variable
add_executable(simple_example ${SOURCE_FILES})
You can directly edit the CMakeLists.txt file in the editor. After editing, make sure to reload the project. By default, the auto-reload feature is disabled, and you can enable it by checking the "Automatically reload CMake project on edit" checkbox in Settings | Build, Execution, Deployment | CMake.
For complex projects, you can create CMakeLists.txt files in subdirectories to describe the build, contents, and target rules of the subdirectories. The target types added by the subdirectory CMakeLists.txt files may vary depending on the role of the module.
CMakeLists.txt Template
When you create a CMake or CMake-based (CUDA, Qt) project through the New Project Wizard, CLion generates CMakeLists.txt using different templates based on the project type and settings. You can fine-tune these templates in Settings | Editor | File and Code Templates under the "Other" tab:
While editing the template text, you can use common predefined variables and the following CMake-specific variables:
${CMAKE_DEFAULT_PROJECT_FILE} Main.cpp/main.c/libary.cpp/library.c files 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 is 3.20, this variable corresponds to 3.
${CMAKE_MINOR_VERSION} The minor number of the minimum supported CMake version. For example, if the version is 3.20, this variable corresponds to 20.
${CMAKE_LIBRARY_TYPE} The type of shared library is SHARED.
${QT_VERSION} The selected Qt version.
${REQUIRED_LIBS} The required Qt libraries for the selected project type. By default, the Qt console executable is Core, and the Qt widget executable is Core, Gui, Widgets.
There is also a template for CMake scripts that you can add to existing projects via the New | CMakeLists.txt in the project view context menu. By default, this template is empty. You can edit it using the same variables in Settings | Editor | File and Code Templates under the "Files" tab:
Code Completion
CLion provides code completion for most elements in CMakeLists.txt. For example, you can get a list of bundled software packages when writing the find_package() command:
Parameter Info Popup
The parameter info popup shows the signature variants of CMake commands as you type:
By default, the popup appears 1 second (1000 milliseconds) after you type the opening parenthesis. You can change this setting in the Parameter Info settings.
To manually invoke parameter info, press Ctrl P.
Quick Documentation Popup
The quick documentation popup helps you get more information about elements in CMake code. To invoke it, hover the mouse or press Ctrl Q.
For example, you can view quick documentation about completion suggestions:
Code Folding
In CMake scripts, you can fold/unfold macros and functions, if-else statements, blocks, comments, and arbitrary code selections. To make this effective, ensure that custom folding regions are enabled in Settings | Editor | General | Code Folding.
Press NumPad -/NumPad + to fold or unfold code snippets.
Structure View
The structure view of CMake shows the variables, functions, macros, and targets used in the script. To open it, press Alt + 7 (for tool window) or Ctrl + F12 (for popup window).
Customizable Syntax Highlighting
You can adjust the color and font scheme of CMake files in Settings | Editor | Color Scheme | CMake:
Code Style for CMake Files
In Settings | Editor | Code Style | CMake, you can adjust the code style applied to CMake files. When changing a setting, the preview pane shows how this setting affects the code.
Popular Recommendations
-
CLion Tutorial – Concise CMake Tutorial in CLion
-
C Language Algorithm – “Color Classification” Algorithm Problem
-
C++ Tutorial – Detailed Explanation of Destructors in C++