Nhfb9E
Nhfb9E

Reputation: 1

CMake project: can't include header from a subfolder

I have a c++ project where I'm trying to take a part of a very large c++ codebase and reuse it for my purposes. I'm trying to set it up via cmake and I'm struggling with different ways to #include headers. The compiler successfully finds simple includes (like #include "a.h") but can't find an include from a subfolder (#include "a/a.h").

I've read the cmake docs and many answers on SO but haven't found an answer so far. Here's a test project with the this problem:

main.cpp
|
CMakeLists.txt
|
Folder_A
|    |
|   a.h
|
Folder_B
|    |
|   b.h
|
Folder_C
    |
    c.h

CMakeLists.txt:

project(test_project)

add_executable(test_project main.cpp)

target_include_directories(test_project PUBLIC "a")
target_include_directories(test_project PUBLIC "b")
target_include_directories(test_project PUBLIC "c")

main.cpp:

#include <iostream>
#include "a.h"


int main() {
    ...
}

a.h:

#include "b.h"
#include "c/c.h"

...

b.h:

#include "c.h"
...

Any help greatly appreciated!

Upvotes: 0

Views: 86

Answers (1)

Sergey Y
Sergey Y

Reputation: 1

Try adding

target_include_directories(test_project PUBLIC .)

along with the other target_include directives, this should add the root path of the project to the include search list and then you should be able to use the "a/a.h" syntax.

Upvotes: 0

Related Questions