Including directories in Clion

Whenever I wanted to include a directory that was outside of my project with Clion, I would use the -I somedir flag. However, this time I want the hierarchy to be like this:

 /project CMakeLists.txt /src /Graph Graph.h Graph.cpp /Dijkstra Dijkstra.h Dijstra.cpp 

I need my code in the /src directory. And not only that, but, for example, inside the Dijkstra.h file, I want to include Graph.h as follows: #include "Graph/Graph.h and not so: #include "../Graph/Graph.h .

If I add only the -I src flag, then if I am inside the Dijkstra.h file, and I would like to include Graph.h , I would have to write #include "../Graph/Graph.h , which is not what I want .

So I also tried adding INCLUDE_DIRECTORIES(src) . This fixed the problem above, however when I tried to compile I got a linker error of undefined reference to...

So, I tried to add the files one by one as follows:

 set(SOURCE_FILES src/Dijkstra/Dijkstra.h src/Dijkstra/Dijkstra.cpp src/Graph/Graph.h src/Graph/Graph.cpp) add_executable(someprojectname ${SOURCE_FILES}) 

and this led to a previous problem when I had to include the following files: #include "../Graph/Graph.h" .

How can I do it right to get the behavior I want?

+5
source share
2 answers

The INCLUDE_DIRECTORIES command INCLUDE_DIRECTORIES not add any source file for compilation!

Instead, this command defines the directories for the search header files .

In any case, you need to specify all the source files in add_executable() :

 include_directories(src) set(SOURCE_FILES src/Dijkstra/Dijkstra.cpp src/Graph/Graph.cpp) add_executable(someprojectname ${SOURCE_FILES}) 
+5
source

UPDATE: @ Tsyvarev's answer is correct. I edited this answer to remove the wrong part and save comments related to target_include_directories() , but it should be considered as additional for Tsyvarev’s answer.

INCLUDE_DIRECTORIES(src) will make the src directory added as a search path to all targets defined from this point. He does not add sources for any purpose. The search path will be relative to the current source directory, and CMake will adjust it accordingly when changing to subdirectories via add_subdirectory() . Although it’s fine, if that’s what you want, as the project becomes more and more complex, you may find that you would prefer to apply the inclusion path parameters only to certain purposes. To do this, use target_include_directories() instead:

 target_include_directories(someprojectname "${CMAKE_CURRENT_SOURCE_DIR}/src") 

This will have the same effect, but restricts the use of the added include path to the target of someprojectname . If you later define another target that does not need an inclusion path, it will not be added. This can help prevent situations such as unexpected files from occurring if you have deep directory hierarchies and, for example, you reuse directory names in different places).

The target_include_directories() command has additional advantages when applied to library objects, because CMake has the ability to transfer the inclusion path to anything that you associate with this library. This doesn't seem like much, but for large projects that define and link many libraries, this can be a huge help. There are other targeted teams that also have similar benefits. This article may give you a little bit of what is possible (disclaimer: I wrote an article). It is more focused on target_sources() , but a discussion about porting dependencies through other targets may be helpful.

+2
source

All Articles