Managing CMake Project Files
If some of your project files include header/source files that do not belong to any CMake target, these files are still recognized by CLion. You will see such files in the Project Tool Window, and CLion's code insight functionality will be fully available.
Let's take a simple example. Suppose the project consists of one source file main.cpp and two header files Header1.h and Header2.h mentioned in CMakeLists.txt:
add_executable(TestProject main.cpp Header1.h Header2.cpp). Also, main.cpp includes another source file IncludeCPP.cpp. As you can see, this file is added to the project tree and is treated by CLion as a regular project file:
When a source or header file is not included in any of the project files or CMake targets, and is not located under the source or library roots, CLion notifies you about it: You can experience an incorrect behavior of such features like syntax highlighting, refactoring, code navigation etc. for such a file until you specify that file as a project one explicitly. To do that use one of the following approaches:
When the source files that don't belong to any target are located within the project root, you should add them to a target. Use
add_library ()CMake commands depending on the target type. Let's consider it for our example.
The above screenshot reflects condition when
add executable ()contains only the
main.cpp Header1.h Header2.hfiles and knows nothing about
add.cpp. Here, you should add the non-project sources to the list of source files, as follows:
add_executable (TestProject main.cpp Header1.h Header2.cpp add.cpp)
When a directory that contains the non-project library sources is located within the project root, you can mark this directory as a library: in the Project tool window, right click the desired directory and select from the context menu.
For the library files located outside the project root, specify the header search paths so that the IDE can index the respective directories and provide code completion and navigation facilities on
For more details, refer to our Quick CMake Tutorial.
Upon any of these actions, the system will prompt you to reload CMake project if automatic reload is not enabled.