在 C++ 中包含头文件时,尖括号 < > 和双引号“” 之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3162030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between angle bracket < > and double quotes " " while including header files in C++?
提问by Sulla
Possible Duplicate:
What is the difference between #include <filename> and #include “filename”?
What is the difference between angle bracket < >
and double quotes " "
while including header files in C++?
在 C++ 中包含头文件时,尖括号< >
和双引号有什么区别" "
?
I mean which files are supposed to be included using eg: #include <QPushButton>
and which files are to be included using eg: #include "MyFile.h"
???
我的意思是应该使用 eg: 包含#include <QPushButton>
哪些文件,使用 eg: 包含哪些文件#include "MyFile.h"
?
回答by Carl Norum
It's compiler dependent. That said, in general using "
prioritizes headers in the current working directory over system headers. <>
usually is used for system headers. From to the specification (Section 6.10.2):
它依赖于编译器。也就是说,通常使用"
当前工作目录中的头优先于系统头。 <>
通常用于系统头文件。从到规范(第 6.10.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.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.
形式的预处理指令
# include <h-char-sequence> new-line
在实现定义的位置序列中搜索由
<
和>
分隔符之间的指定序列唯一标识的标头,并导致该指令被标头的全部内容替换。如何指定位置或标识的标头是实现定义的。形式的预处理指令
# include "q-char-sequence" new-line
导致用
"
分隔符之间的指定序列标识的源文件的全部内容替换该指令。以实现定义的方式搜索命名的源文件。如果不支持此搜索,或者如果搜索失败,则重新处理指令,就好像它读取一样# include <h-char-sequence> new-line
具有与
>
原始指令相同的包含序列(包括字符,如果有)。
So on mostcompilers, using the ""
first checks your local directory, and if it doesn't find a match then moves on to check the system paths. Using <>
starts the search with system headers.
因此,在大多数编译器上,使用第""
一个检查您的本地目录,如果没有找到匹配项,则继续检查系统路径。使用使用<>
系统标题开始搜索。
回答by Fabio Ceconello
When you use angle brackets, the compiler searches for the file in the include path list. When you use double quotes, it first searches the current directory (i.e. the directory where the module being compiled is) and only then it'll search the include path list.
使用尖括号时,编译器会在包含路径列表中搜索文件。当您使用双引号时,它首先搜索当前目录(即正在编译的模块所在的目录),然后才搜索包含路径列表。
So, by convention, you use the angle brackets for standard includes and the double quotes for everything else. This ensures that in the (not recommended) case in which you have a local header with the same name as a standard header, the right one will be chosen in each case.
因此,按照惯例,标准包含使用尖括号,其他所有内容使用双引号。这可确保在(不推荐)具有与标准标头同名的本地标头的情况下,将在每种情况下选择正确的标头。