48

I have looked in The C++ Programming Language to try to find the answer to this. When I #include "my_dir/my_header.hpp" in a header, where does it look for this file? Is it relative to the header, relative to the source file that included it, or something else?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
rlbond
  • 65,341
  • 56
  • 178
  • 228
  • 1
    Possible duplicate of [What is the difference between #include and #include "filename"?](http://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename), that is a superset. – Ciro Santilli OurBigBook.com Apr 15 '16 at 08:05

6 Answers6

28

Implementation defined. See what is the difference between #include <filename> and #include “filename”.

Community
  • 1
  • 1
aib
  • 45,516
  • 10
  • 73
  • 79
10

It is relative to both the current source file and to any search paths given (-I for gcc).

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
8

It depends on what syntax you use in the #include directive:

#include "path-spec"
#include <path-spec>

Quoted form : This form instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

Angle-bracket form : This form instructs the preprocessor to search for include files first along the path specified by the /I compiler option, then, when compiling from the command line, along the path specified by the INCLUDE environment variable.

The path-spec is a filename optionally preceded by a directory specification. The filename must name an existing file. The syntax of the path-spec depends on the operating system on which the program is compiled.

This information should be in the documentation for your specific C++ Preprocessor Reference, the above is taken from this article on MSDN which has more on the subject.

Peter McG
  • 18,857
  • 8
  • 45
  • 53
  • 2
    You should state which implementation (compiler) this is. – aib Mar 14 '09 at 02:07
  • 3
    @aib: Thanks - The info is from the referenced MSDN link and is specific to Microsoft's Visual Studio 2005 preprocessor. The (very similar) Visual Studio 2008 version can be found at http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx – Peter McG Mar 14 '09 at 05:37
  • 1
    Does "-and then in the directories of any files that include (#include) that file.", mean that `#include "foo/bar.h" will search the directories of all files in the project that include bar.h? The wording confuses me a little, so I just want to be a 100% clear. – batbrat Jan 16 '17 at 09:15
  • 1
    @batbrat: For "nested" includes it searches: "In the directories of the currently opened include files, in the reverse order in which they were opened. The search begins in the directory of the parent include file and continues upward through the directories of any grandparent include files." – Vladimir Shutow Apr 03 '19 at 20:03
3

The complete search path may depend on the compiler. For Visual Studio, the documentation states that it:

(...) instructs the preprocessor to look for include files in the same directory of the file that contains the #include statement, and then in the directories of any files that include (#include) that file. The preprocessor then searches along the path specified by the /I compiler option, then along paths specified by the INCLUDE environment variable.

cgmb
  • 4,284
  • 3
  • 33
  • 60
noup
  • 738
  • 2
  • 11
  • 19
1

Its implementation defined. Those #include"my_dir/xxy.hpp" on a file (for example foo.h) are relative to the file (foo.h and my_dir would be on the same level at the directory hierarchy). With some (most?) compilers, you can use a flag to use these < > (#include

I know that gcc / g++ provides the -I flag. So you could use g++ -I /home [...] indicating that the xxy.hpp file is located in the /home/my_dir/ directory. I havent used any other C/C++ compiler in a while now.

Tom
  • 43,810
  • 29
  • 138
  • 169
0

for GCC version <= 3.0, the angle-bracket form does not generate a dependency between the included file and the including one. So if you want that your makefile automatically generates dependencies, you must use the quoted form for the files that should be included in the dependency tree.

Denis Ros
  • 229
  • 2
  • 2