头文件顺序
可能的重复:
C++ 标头顺序
// Here is module This.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <math.h>
#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
#include <list>
#include <string>
#include <stdio.h>
#include "stdafx.h" // for precomp
#include <tchar.h>
#include "This.h"
#include <Windows.h>
#include <Winsock2.h>
现在按字母顺序排列。
我正在寻找订购它们的最佳实践。 (有一个好的公式吗?)
您将如何排序这些头文件?为什么?
Possible Duplicate:
C++ Header order
// Here is module This.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <math.h>
#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"
#include <list>
#include <string>
#include <stdio.h>
#include "stdafx.h" // for precomp
#include <tchar.h>
#include "This.h"
#include <Windows.h>
#include <Winsock2.h>
They are ordered by alphabet now.
I'm finding the best practice for ordering them. (Is there a nice formula?)
How will you order these header files? Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
应尽可能将标头写入:1)独立于可能已包含的内容,2)不会为稍后包含的标头引入问题(例如通用标识符的宏)。当这两者都成立时,包含的顺序并不重要。如果这些不正确,您应该在自己的标头中修复它,或者根据需要进行其他处理。
因此,选择是任意的,但做出选择仍然很重要! “计划不算什么,但计划就是一切。”一致性会带来更具可读性的代码。
一种相对常见的顺序是标准库、系统(如操作系统)、外部库,然后是与当前文件相同的项目的标头 - 一个例外是实现文件,包括其“相应”标头(如果有的话)首先,在任何包含或其他代码之前。在每个组中,我按照习惯按字母顺序排序,因为它完全是任意的,但一些易于使用的顺序让我可以快速阅读和更新列表。
应用于您的列表:
上面用于分组的换行符是故意的。我会留下评论为什么winsock2有自己的组,但其余的评论通常不会在那里。
Headers should be written to, as much as possible, 1) be independent of what may have already be included, and 2) not introduce problems (such as macros for common identifiers) for headers later included. When both of these are true, the order in which you include doesn't matter. If these aren't true, you should fix that in your own headers, or deal with it as necessary otherwise.
Thus, the choice is arbitrary, but it is still important that you make a choice! "The plan is nothing, but the planning is everything." Consistency leads to more readable code.
A relatively common order is standard library, system (as in OS), external libraries, and then headers for the same project as the current file – with the one exception of an implementation file including its "corresponding" header (if there is one) first, before any includes or other code. Within each group, I sort alphabetically by habit, because, again, it's completely arbitrary but some easy to use order lets me quickly read and update the list.
Applied to your list:
The line breaks above to split into groups are deliberate. I would leave a comment on why winsock2 is given its own group, but the rest of the comments would not normally be there.
这很大程度上取决于您如何需要将它们包括在内。如果没有一个依赖于另一个,那么您可以相对自由地使用自己的方案来包含(一般来说,如果另一个需要,最重要的头文件将被隐式包含)。
但请注意,WinSock2.h 必须包含在 Windows.h 之前,否则很有可能在编译时出现链接器错误。
祝你好运!
丹尼斯·M.
It is highly dependent on how you need them included. If none are dependent on another, you are relatively free to include using your own scheme (generally speaking, most important header files will be implicitly included if required by another).
Please note, however, that WinSock2.h must be included before Windows.h or else there is a high possibility that you will get a linker error at compile time.
Good luck!
Dennis M.
头文件的放置顺序并不重要。这主要是一个选择问题。许多人首先放置系统标头(<> 之间的标头),然后放置私有标头,但这取决于您。
It doesn't really matter what order you put your header files. This is mostly a matter of choice. Many people put the system headers (the ones between <>) first and the private headers afterwards, but it's up to you.
标准库,然后是不在标准库中的系统头文件,然后是私有头文件。用空行分隔每个类别。评论任何需要解释的内容。
如果您有依赖项,请在依赖于它们的标头之前包含标头所依赖的标头 (doi!)。
Standard library, then system headers not in the standard library, then private headers. Separate each category by a blank line. Comment anything that needs explanation.
If you have dependencies, include the ones that headers depend on before the headers that depend on them (doi!).