At开发者_如何学Go this tutorial it mentions the following about #include "filename"
:
#include "filename" tells the compiler to look for the file in directory containing the source file doing the #include. If that fails, it will act identically to the angled brackets case.
What is meant by the bolded font sentence?
Thanks.
The bold bit simply means that, if the file specified inside quotes cannot be located using the "
method, it will revert to the <>
method.
I should mention that the bit about where it looks for the include
files is actually incorrect. In both cases (quotes and angle brackets), the search locations are implementation defined.
From the lex.header
section:
The sequences in both forms of header-names are mapped in an implementation-defined manner to headers or to external source file names as specified in 16.2.
The 16.2
section follows:
A #include
directive shall identify a header or source file that can be processed by the implementation.
A preprocessing directive of the form
# include < h-char-sequence> new-line
searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the <
and >
delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.
A preprocessing directive of the form
# include " q-char-sequence" new-line
causes the replacement of that directive by the entire contents of the source file identified by the specified sequence between the "
delimiters. The named source file is searched for in an implementation-defined manner. If this search is not supported, or if the search fails, the directive is reprocessed as if it read
# include < h-char-sequence> new-line
with the identical contained sequence (including >
characters, if any) from the original directive.
So the statement "... tells the compiler to look for the file in directory containing the source file doing the #include ..."
is wrong. It's totally up to the implementation how it finds the files, in both cases.
Having said that, the rest is correct. If the method used by the "
type does not locate the header, the method used by the <>
type is then used. That's really all the bold bit means.
You just have to read the documentation for your particular implementation to see what those methods are.
While the exact details are implementation-dependent, there are a few common practices. In most common compilers, using the quotes #include "filename.h"
searches the current directory by default. Using angle brackets #include <filename.h>
searches system-defined library directories. What it is saying is that if the current directory doesn't have the file you need, it will search the system directories instead.
Note that some compilers may be different, and your compiler itself may have options to change these directories. There is also the possibility that system headers don't actually exist, but that #include <foo.h>
is directly recognized by the compiler to enable certain built-in definitions.
精彩评论