CLion 2020.1 Help

Manage CMake Project Files

Add new files

  1. In the Project tree, right-click the folder you want to add a file into and select New from the context menu. Choose the desired file type:

    Adding new files from the Project view

  2. Specify the name, type, and additional options for the new file.

    For C++ Class, C/C++ Source, and C/C++ Header file templates, CLion will prompt you to add the new file to one or several existing CMake targets:

    Adding a new file to an existing CMake target

  3. If you have selected the target on the previous step, the new entry will be automatically added to CMakeLists.txt:

    New file added to the selected CMake target

Manage included files

  • When you include a header or source file located under the project root into any project file, CLion treats it as a project file as well. There is no need to list such includes manually in CMakeLists.txt.

    CMake project: included files

Add non-included files

When a source or header file is not included in any of the project files or CMake targets, CLion notifies you about it:

Warning on file not belonging to any target

In this case, you need to specify the file as a project one manually. Use one of the following approaches depending on the file location:

  • If the file is located under the project root, add it to a target using add_executable() or add_library() command. For example:

    Before

    add_executable (TestProject main.cpp)

    After

    add_executable (TestProject main.cpp new_file.cpp)

    When the directory that contains non-project sources is located under the project root, you can also mark it as library: right-click in the Project view and select Mark Directory As | Library Files from the context menu.

  • To use library files located outside the project root, you need to specify header search paths so that CLion can index the directories and provide completion and navigation for the #include statements:

    include_directories( ${MY_EXTERNAL_SOURCE__DIR})

    Refer to Quick CMake Tutorial: Adding include directories for more information.

Upon any of these actions, CLion will prompt you to reload the project (when automatic reload is disabled):

Reloading the project after adding a new file

Last modified: 08 May 2020