C++ #include 的排序
Ordering of C++ #include's
这是来自 Google's C++ style guide 的关于#include 的部分:
In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement
or test the stuff in dir2/foo2.h, order your includes as follows:
dir2/foo2.h.
A blank line
C system files.
C++ system files.
A blank line
Other libraries' .h files.
Your project's .h files.
Note that any adjacent blank lines should be collapsed.
With the preferred ordering, if dir2/foo2.h omits any necessary
includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus,
this rule ensures that build breaks show up first for the people
working on these files, not for innocent people in other packages.
我不明白最后一行:
Thus, this rule ensures that build breaks show up first for the people
working on these files, not for innocent people in other packages.
谁能解释一下如何将 dir2/foo2.h
放在第一个会导致 "good" 构建中断,以及将 dir2/foo2.h
最后放置会如何导致 "bad" 构建中断?
"good" 构建中断是指进行重大更改的人中断的时间。因为那家伙可以修。如果那个人 没有 注意到那些破坏性的变化,因为包含文件是排序的,所以它确实为他编译,那么它就会被释放。
"bad" 构建中断是指发布的版本中断了该代码用户的构建。
在 dir2.h
中,您忘记包含 X.h
。然后在当前文件中包括:
X.h
dir2.h
这将编译正常。然后其他人在其他地方包含 dir2.h
,他们最终会出现源自 dir2.h
的编译错误,即使他们从未更改该文件中的任何内容...
如果您的顺序正确,您应该会在第一次包含 dir2.h
时收到错误消息。
这是来自 Google's C++ style guide 的关于#include 的部分:
In dir/foo.cc or dir/foo_test.cc, whose main purpose is to implement or test the stuff in dir2/foo2.h, order your includes as follows:
dir2/foo2.h. A blank line C system files. C++ system files. A blank line Other libraries' .h files. Your project's .h files.
Note that any adjacent blank lines should be collapsed.
With the preferred ordering, if dir2/foo2.h omits any necessary includes, the build of dir/foo.cc or dir/foo_test.cc will break. Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages.
我不明白最后一行:
Thus, this rule ensures that build breaks show up first for the people working on these files, not for innocent people in other packages.
谁能解释一下如何将 dir2/foo2.h
放在第一个会导致 "good" 构建中断,以及将 dir2/foo2.h
最后放置会如何导致 "bad" 构建中断?
"good" 构建中断是指进行重大更改的人中断的时间。因为那家伙可以修。如果那个人 没有 注意到那些破坏性的变化,因为包含文件是排序的,所以它确实为他编译,那么它就会被释放。
"bad" 构建中断是指发布的版本中断了该代码用户的构建。
在 dir2.h
中,您忘记包含 X.h
。然后在当前文件中包括:
X.h
dir2.h
这将编译正常。然后其他人在其他地方包含 dir2.h
,他们最终会出现源自 dir2.h
的编译错误,即使他们从未更改该文件中的任何内容...
如果您的顺序正确,您应该会在第一次包含 dir2.h
时收到错误消息。