可能的重复: #include <filename>和#include " filename "有什么区别?

在c++中包含头文件时,尖括号< >和双引号" "之间有什么区别?

我的意思是哪些文件应该包括使用eg: #include <QPushButton>和哪些文件将包括使用eg: #include“MyFile.h”??


它依赖于编译器。也就是说,通常使用“将当前工作目录中的头文件优先于系统头文件”。<>通常用于系统头文件。从到规范(第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使用尖括号,其他所有内容使用双引号。这可以确保在(不推荐)有与标准标头名称相同的本地标头的情况下,在每种情况下都会选择正确的标头。