views:

837

answers:

5

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?

+7  A: 

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

aib
+3  A: 

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

Ignacio Vazquez-Abrams
+1  A: 

The complete search path may depend on the compiler. In Visual Studio:

(...) 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.

noup
+1  A: 

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 McGrattan
You should state which implementation (compiler) this is.
aib
@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 McGrattan
+1  A: 
Tom