#include和#include" filename"有什么区别?
在C和C ++编程语言中,在" include"语句中使用尖括号和引号有什么区别,如下所示?
- #include <文件名>
#include"文件名"
解决方案
回答
实际上,区别在于预处理器搜索包含文件的位置。
对于" #include <文件名>",预处理器通常以编译器/ IDE预先指定的搜索目录以与实现方式相关的方式进行搜索。此方法通常用于包括标准库头文件。
对于#include" filename"
,预处理器首先在与包含指令的文件相同的目录中搜索,然后遵循用于#include <filename>格式的搜索路径。此方法通常用于包括程序员定义的头文件。
有关搜索路径的GCC文档中提供了更完整的描述。
回答
我相信,如果在当前目录中找不到双引号中包含的标头,则它们将与尖括号包含的标头在相同的系统路径中查找。
回答
<file>
包括告诉预处理器先在-I目录和预定义目录中搜索,然后在.c文件的目录中搜索。 " file"包括告诉预处理器首先搜索源文件的目录,然后恢复为-I和预定义的目录。无论如何,都会搜索所有目的地,只是搜索顺序不同。
2011年标准主要讨论" 16.2源文件包含"中的包含文件。
2 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. 3 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.
请注意,如果找不到该文件,则" xxx"格式降级为" <xxx>"格式。其余的是实现定义的。
回答
<和>之间的字符序列唯一地指的是标头,不一定是文件。实现几乎可以随意使用它们的字符序列。 (不过,大多数情况下,只需将其视为文件名,并在其他帖子中注明在包含路径中进行搜索即可。)
如果使用" #include" file"形式,则实现将首先查找具有给定名称的文件(如果支持)。如果不(不支持),或者搜索失败,则该实现的行为就好像使用了其他形式(
#include <file>`)一样。
另外,当#include
指令与以上两种形式都不匹配时,将使用第三种形式。在这种形式中,对#include指令的"操作数"进行了一些基本的预处理(例如宏扩展),并且预期结果将与其他两种形式之一匹配。
回答
唯一知道的方法是阅读实现的文档。
在C标准的6.10.2节中,第2至4段规定:
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. A preprocessing directive of the form #include pp-tokens new-line (that does not match one of the two previous forms) is permitted. The preprocessing tokens after include in the directive are processed just as in normal text. (Each identifier currently defined as a macro name is replaced by its replacement list of preprocessing tokens.) The directive resulting after all replacements shall match one of the two previous forms. The method by which a sequence of preprocessing tokens between a < and a > preprocessing token pair or a pair of " characters is combined into a single header name preprocessing token is implementation-defined. Definitions: h-char: any member of the source character set except the new-line character and > q-char: any member of the source character set except the new-line character and "