Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including directories in Clion

Tags:

c++

cmake

clion

Whenever I wanted to include a directory that was located outside of my project with Clion I would use the -I somedir flag. This time however, what I want to do is to have a hierarchy like this:

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

I want my code in a /src directory. And not only that, but also, for example, inside the file Dijkstra.h I want to include the Graph.h like this: #include "Graph/Graph.h and not like this: #include "../Graph/Graph.h.

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

So I tried to also add INCLUDE_DIRECTORIES(src). That fixed the problem above, however when tried to compiled, I got a linker error undefined reference to....

So I tried adding the files one by one like this:

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 that brought back the previous problem, where I had to include the files like this: #include "../Graph/Graph.h".

How can I do this properly to get the behavior I want ?

like image 323
dimitris93 Avatar asked Apr 27 '16 09:04

dimitris93


1 Answers

Command INCLUDE_DIRECTORIES doesn't add any source file for compile!

Instead, this command defines directories for search header files.

You need to list all source files in add_executable() call in any case:

include_directories(src)
set(SOURCE_FILES
    src/Dijkstra/Dijkstra.cpp
    src/Graph/Graph.cpp)
add_executable(someprojectname ${SOURCE_FILES})
like image 149
Tsyvarev Avatar answered Sep 21 '22 22:09

Tsyvarev