c++ - #include "xxx.h"与 #include <xxx.h> 的规则是什么?

标签 c++ header-files

<分区>

如果我有自己的库项目,我应该使用哪种样式来#include 我的应用程序中的 header ?是否有严格的规定,这两者对编译器/预处理器实际上有不同的含义还是仅与标准有关?

最佳答案

根据 ISO 标准,规则很少。这两种形式在查找头文件的位置方面都依赖于实现。它们甚至不必是 文件。

2.9除了可以包含 " 之外,C++11 对这两个变体没有区别。在<>变体和 >""变体,但很少有人会愚蠢到在文件名中使用这些字符:-)

16.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.


我倾向于使用 <>用于系统标题和 ""对于我自己的标题,但这只是个人喜好。我会注意到上述 C++11 文档指出:

Note: Although an implementation may provide a mechanism for making arbitrary source files available to the <> search, in general programmers should use the <> form for headers provided with the implementation, and the "" form for sources outside the control of the implementation.

这不是强制性的,但它仍然是一个好主意。

关于c++ - #include "xxx.h"与 #include <xxx.h> 的规则是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4118376/

相关文章:

c++ - std::string — 小字符串优化和交换

c++ - Qt Creator 拒绝将自定义 python 程序作为构建步骤命令执行

xcode - 我不知道如何将头文件更改为 "public"

c++ - 包含一个大标题以将对象用作默认参数

c++ - 将 POCO 静态库嵌入另一个

c++ - 如何从函数指针推导出参数列表?

c++ - 使用成员变量的地址作为ID

c++ - vector 删除错误

c++ - C header 的重复符号问题

C++:没有 <cstring> 的字符串函数