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:
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 setting the Automatically reload CMake project on editing checkbox in .
Subdirectory CMakeLists.txt files
For projects with complex structure, you can create subdirectory CMakeList.txt files to describe the build, contents, and target rules for the subproject.
The type of the target added by the subdirectory CMakeLists.txt can vary depending on the role of the module.
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 , the Other tab:
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 |
${CMAKE_MINOR_VERSION} | the minor number of the minimum supported CMake version. For example, if the version if |
${CMAKE_LIBRARY_TYPE} |
|
${QT_VERSION} | the selected Qt version. |
${REQUIRED_LIBS} | the Qt libraries required for the selected project type. By default, they are |
There is also a template for CMake scripts that you can add to an existing project via Files tab:
in the context menu of the project view. By default, this template is empty. You can edit it using the same variables in , theCoding assistance in CMakeLists.txt
Code completion
CLion provides code completion for most of the elements in your CMakeLists.txt. For example, you can get the list of the packages bundled with CMake when writing a find_package()
command:
Quick fix for CMake minimal version
You can quickly adjust the minimal required version of CMake. Locate the warning in CMake output and click Fix:
CLion will change the version in the cmake_minimum_required()
command:
Adding targets via the Generate menu
Press Alt+Insert and select the target type:
Choose the files to be added to the new target:
CLion will add an
add_executable
oradd_library
command:
Live templates for CMake
Use live templates to quickly insert a structure or a command. For example, type exe
and press Tab to add an executable target:
Find the full list of CMake live templates in
.Parameter info popup
The Parameter Info popup shows signature variants for CMake commands as you type:
By default, the popup appears within 1 second (1000 milliseconds) after you type an opening bracket. You can change this in the Parameter Info settings.
To invoke parameter info manually, press Ctrl+P.
Quick documentation popup
Quick Documentation popup helps you get more information on the elements in your CMake code. To invoke it, use mouse hover or press Ctrl+Q.
For example, you can view quick documentation for completion suggestions:
Code folding
Folding/unfolding in CMake scripts is available for macros and functions, if-else clauses, blocks, comments, as well as arbitrary code selections (make sure to enable Custom folding regions in for this to work).
Structure view
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).
Customizable syntax highlighting
You can adjust the color and font scheme for CMake files in
:Code style for CMake files
In
, you can adjust the code style being applied to CMake files. When you change a certain setting, the Preview pane shows how this will affect your code.