针对此问题,我只关心符合标准的C++,不包括C或C++0x,并且不涉及任何特定于实现的细节。
有时会出现关于#include ""
和#include <>
之间差异的问题。争论通常归结为两个差异:
- 具体实现通常在两种形式的不同路径中搜索。这是特定于平台的,不在此问题的范围内。
- 标准规定
#include <>
用于“头文件”,而#include ""
用于“源文件”。以下是相关参考:
在上述引文中,我加粗的强调表明这种差异的含义似乎是标准意图区分“头文件”和“源文件”,但是该文件没有定义这些术语或它们之间的差异。几乎没有其他地方提到头文件或源文件。其中一处是:头文件不一定是源文件,头文件名称中由“<”和“>”分隔的序列也不一定是有效的源文件名称(16.2)。这似乎意味着头文件可能不驻留在文件系统中,但它也没有说源文件需要这样做。另一处是:“2词法约定[lex] 1程序的文本以国际标准中称为源文件的单位保存。通过预处理指令#include包括所有标题(17.4.1.2)和源文件(16.2)的源文件,减去任何条件包含(16.1)预处理指令跳过的源代码行,称为翻译单位。[注意:C ++程序不必同时全部翻译。] ”这是我能找到的最接近定义的内容,它似乎意味着头文件不是“程序文本”。但是,如果您使用#include包括头文件,它是否会成为程序文本的一部分?这有点误导人。那么什么是头文件?什么是源文件?ISO/IEC 14882:2003(E)
16.2 Source file inclusion [cpp.include]
1 A #include directive shall identify a header or source file that can be processed by the implementation.
2 A preprocessing directive of the form
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.
# include < h-char-sequence > new-line
3 A preprocessing directive of the form
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 "q-char-sequence" new-line
with the identical contained sequence (including > characters, if any) from the original directive.
# include < h-char-sequence > new-line