Quasar
Quasar

Reputation: 295

Multiple includes with #include in C++

I have a question regarding the #include preprocessor directive in C++.

In my program I would like to include a header file from another directory. For this I have used the full path, example:

#include "full/path/to/my/header.hpp"

Now it turns out that header.hpp itself has an include (let's say #include "otherheader.hpp". During compilation, the compiler complains that it cannot find this other header.

What is the best way to handle this problem, considering the fact that I also don't want to write the full path for every header file, especially including those that are only needed "further down the tree"?

Upvotes: 0

Views: 2318

Answers (3)

Ronan Boiteau
Ronan Boiteau

Reputation: 10138

You should use your compiler's -I option.

Example with g++:

g++ -I full/path/to/my/

Then in your code you can simply put:

#include "header.hpp"

More information on search path.

Upvotes: 7

Jesper Juhl
Jesper Juhl

Reputation: 31465

Don't specify full path to includes. Specify include directories to the compiler instead and then just #include <foo> knowing that foo will be found in /some/path/foo because you told the compiler to search /some/path/ for includes. For many compilers this is done with the -I option.

Upvotes: 1

Sean
Sean

Reputation: 62472

Most compilers allow you to specify the root for additional include directories when compiling. For example, in Visual C++ you specify the -I flag. It's also the same for gcc. For example:

compiler -Ipath/to/headers myfile.c

Upvotes: 4

Related Questions