EXIT_FAILURE
EXIT_FAILURE

Reputation: 288

add root directory as include directory

During compilation certain header files aren't found, although I added the respective include directories in CMake. Unfortunately this is the code and directory structure I am stuck with and I can't change anything on the include statements.

I have the following directory structure:

projectfolder
+--source1
|  |--prog.cpp
|  |--anotherHeader.h
|  |--CMakeLists
|
+--includefolder
|  +--source1_include
|  |  |--header.h
|  
|--CMakeLists

CMakeLists in projectfolder looks like this:

project (project)
include_directories(includefolder)
add_subdirectory(source1)

prog.cpp has:

#include  "source1_include/header.h"

and header.h has:

 #include  "anotherHeader.h"

(don't ask me why, I don't know myself, maybe it has something to do with the fact that originally this is a Visual Studio project)

I thought I could fix this, by adding

include_directories(.)

to the CMakeLists in the source1 directory but unfortunately it wont work that way. anotherHeader.h isn't found.

Upvotes: 1

Views: 2177

Answers (1)

Kenny Ostrom
Kenny Ostrom

Reputation: 5871

I'm assuming that "." is "projectfolder"

If I understand correctly, you added -I "projectfolder", so now "source1_include/header.h" correctly finds "projectfolder/source1_include/header.h"

Now, "header.h" tries to include "anotherHeader.h", which is not in its folder and not in any of the included folders. It is actually in "source1". So cmake is correct to error out.

You need to either move "anotherHeader.h" into your includes folder (my recommendation), or edit "header.h" to find it by the correct relative path "../source1/anotherHeader.h" (not recommended), or add include_directories("source1"), which is where it actually is.

Upvotes: 1

Related Questions