CLion 2023.3 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 a source file
  2. Specify the name, type, and additional options for the new file.

    Adding a file to an existing target
  3. For C++ Class, C/C++ Source, C/C++ Header, and C++ Module Interface Unit templates, CLion will prompt you to add the new file to CMake targets. You can select existing targets or create new targets.

    To add a new CMake target:

    1. Click the Add new target link:

      Adding a file to a new target
    2. Specify the target type, name, and location. Then click Add:

      Adding a new executable target
    3. The newly created target will appear in the list.

      New target added to the list of targets

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.

Add existing files to CMake targets

To add a file which is not included in any of the project files or CMake targets, do one of the following:

  • Click Add to CMake Project in the top-right corner of the editor:

    Add file to CMake project

    You can select existing targets or create new targets.

  • Select one or several files in the Project tool window and choose Add <filename> to CMake Project from the context menu:

    Add file to CMake project from context menu
  • You can also add the files to the CMake project manually.

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

  • If the directory containing 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})

    Where MY_EXTERNAL_SOURCE__DIR is the required external path.

    For more information, refer to Quick CMake Tutorial: Adding include directories.

Last modified: 15 March 2024