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