-1

There are already many options listed of how to add a path to a C++ compiler so that the #include <...> command works on these paths. However, assume that I have a single file (not an entire project) and I want to add an include path just for this file alone. I'd like to do this via a line of code within the cpp-file (say, as very first line). How is this possible? Why? Because I need to include some header file from another directory which in turn depends also on other header files within that same directory (and I get error messages that these other files could not be found due to the fact that this path was not added to the include-list).

For example: Let's assume I want to include file_a.h in directory .../include/extra,

I can do that via

#include <extra/file_a.h> 

However, if, e.g., I do not have the extra-directory directly as a sub-directory of include, or the file_a wants to include some other file from somewhere else (maybe even /extra, but it's not a sub directory of include e.g.), then I run into trouble, because then the tracking of directories/dependencies gets hard.

I thought it would be a bad habit to change those directories via compiler, so I thought better solution would be to integrate it into the program, so no matter which compiler I use, it would work anyway without even having to think about afterwards, once specified, which directories I have to add.

LPs
  • 16,045
  • 8
  • 30
  • 61
Yinyue
  • 145
  • 1
  • 3
  • 12
  • If you can change the files you're including, you could change their includes from `` to `"file.h"` http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename – Lanting May 08 '17 at 06:11
  • Because you are probably not thinking of a code line which gets executed at runtime, what do you mean by "code line"? Why are you not using paths inside your #include statements? Please describe more about what you want to achieve otherwise you are in danger of receiving "XY problem" comments. Please show examples of code and headers and describe the desired effect. In short try for a [mcve]. – Yunnosch May 08 '17 at 06:12
  • This is kind of a chicken and egg problem ;-) – Jabberwocky May 08 '17 at 06:50
  • Sorry for being too unspecific, I've modified my question. – Yinyue May 09 '17 at 06:15

3 Answers3

3

Per my understanding you did:

#include <absolute/path/to/header/header.h

or

#include <relative/path/to/header/header.h

But into the header.h some other include are included.

#include <header_1.h>
#include <header_2.h>
[...]
#include <header_n.h>

Those other headers haven't relative/absolute path, so compiler doesn't know how to find them.

To solve this you can use (using ) the -I compiler option:

-I dir

Add the directory dir to the list of directories to be searched for header files during preprocessing. [...]

Emphasis mine

So you can use

#include <header.h>

In your file and compile it using

gcc ... -I/path/to/headers ...
Community
  • 1
  • 1
LPs
  • 16,045
  • 8
  • 30
  • 61
  • 1
    You are guessing at what OP does/wants. That is brave. But also you seem to contradicat what OP states (and probably believes) to need "via a line of code within the cpp-file". So you are guessing that OP wants something else than explicitly stated. I recommend to explain why you go against what OP has (even in bold letters) said in the question. But convincing OP to change the approach is more likely to succeed interactively. As it is, the answer looks very much like not being an answer to the question. – Yunnosch May 08 '17 at 06:39
  • @Yunnosch Well, can be right. We'll see. In case OP's problem is different and my guesses are wrong I'll delete my answer: no problem. – LPs May 08 '17 at 06:53
  • @Yinyue My solution (compiler based) is the only one you can follow. Otherwise you must move all your headers inside your project directory and modify all `#include` directive to match your project tree. – LPs May 09 '17 at 06:21
  • Thanks, well, it appears that this is indeed the only option. – Yinyue May 09 '17 at 12:16
2

When you have to specify one or more include paths in the compile command, you can do it as follows:

g++ -I/path1/to/headers -I/path2/to/headers YourProgram.cpp
Andrushenko Alexander
  • 1,839
  • 19
  • 14
1

Include paths tell the compiler where it finds the files it actually shall include into other files. This is (usually) controlled via compiler options as LPs explained in this answer.

C++ standard does not provide any facilities to do this from within a C++ source file and I am not aware either of any compiler extension of any vendor that would allow doing so, so bad luck...

Now depending on the IDE you are using (hopefully you are using one...), though, you most likely can add include paths for files individually there (would be a strange IDE if it didn't allow...), e. g. with eclipse + GCC, right click the file, select "Properties" -> C/C++ Build -> Tool Settings -> GCC C++ Compiler -> Includes.

Alternatively you could use a make file instead (actually, eclipse in standard settings generates one for you automatically...), which again allows you to set compiler options for each file individually - either written directly by yourself or generated by some other tools facilitating this such as cmake.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59