两种include的区别

 #include< >和#include“”的区别

< >引用的是编译器的类库路径里面的头文件

" "引用的是程序目录的相对路径中的头文件


作者:暮无井见铃

链接:https://www.zhihu.com/question/25436617/answer/202700196

来源:知乎

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

从标准来看

n4659, 19.2 Source file inclusion, 2

A preprocessing directive of the form

# include < h-char-sequence >

searches a sequence of implementation-defined places for a headeridentified uniquely by the specified sequence between the < and >delimiters, and causes the replacement of that directive by the entire contentsof the header. How the places are specified or the header identified isimplementation-defined.


拥有此形式的预处理指令

# include < h-char-sequence >

于一序列实现定义位置,查找由 < 与 > 分隔符之间的序列所唯一标识的头文件,并导致该指令为头文件的整体内容所替换。位置如何指定,或头文件如何标识,是实现定义的。


n4659, 19.2 Source file inclusion, 3

A preprocessing directive of the form

# include " q-char-sequence "

causes the replacement of that directive by the entire contents ofthe source file identified by the specified sequence between the "delimiters. The named source file is searched for in an implementation-definedmanner. If this search is not supported, or if the search fails, the directiveis reprocessed as if it read

# include < h-char-sequence >

with the identical contained sequence (including > characters, ifany) from the original directive.


拥有此形式的预处理指令

# include " q-char-sequence "

导致该指令为 " 分隔符间的序列所标识的源文件的整体内容所替换。指名的源文件以实现定义行为查找。若不支持此查找,或若查找失败,则此指令以如同它从原指令,以相同的所含序列读取

# include < h-char-sequence >(包含 > 字符,若它存在)。


(C11 标准里也有一模一样的话)

简言之 #include <> 和 #include "" 都会在实现定义的位置查找文件,并将其包含。

区别是若 #include "" 查找成功,则遮蔽 #include <> 所能找到的同名文件;否则再按照 #include<> 的方式查找文件。另外标准库头文件都放在 #include <> 所查找的位置。

一般来说 #include <> 的查找位置是标准库头文件所在目录, #include "" 的查找位置是当前源文件所在目录。不过这些都可由编译器调用参数等配置更改。

猜你喜欢

转载自blog.csdn.net/hq1097/article/details/80802694