头文件顺序

发布于 2024-10-14 21:19:14 字数 637 浏览 3 评论 0原文

可能的重复:
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一抹苦笑 2024-10-21 21:19:14

应尽可能将标头写入:1)独立于可能已包含的内容,2)不会为稍后包含的标头引入问题(例如通用标识符的宏)。当这两者都成立时,包含的顺序并不重要。如果这些不正确,您应该在自己的标头中修复它,或者根据需要进行其他处理。

因此,选择是任意的,但做出选择仍然很重要! “计划不算什么,但计划就是一切。”一致性会带来更具可读性的代码。

一种相对常见的顺序是标准库、系统(如操作系统)、外部库,然后是与当前文件相同的项目的标头 - 一个例外是实现文件,包括其“相应”标头(如果有的话)首先,在任何包含或其他代码之前。在每个组中,我按照习惯按字母顺序排序,因为它完全是任意的,但一些易于使用的顺序让我可以快速阅读和更新列表。

应用于您的列表:

// first only because this is required in your precompiled header setup
#include "stdafx.h"

// it's too bad this can't really be first
// I'm guessing "This" refers to the corresponding header
#include "This.h"

// C stdlib then C++ stdlib is what I usually do,
// whether the C headers are spelled XXX.h or cXXX.
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>

// someone mentioned winsock2 needs to be before windows
#include <Winsock2.h>

#include <tchar.h>
#include <Windows.h>

#include <boost/regex.hpp>

#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"

上面用于分组的换行符是故意的。我会留下评论为什么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:

// first only because this is required in your precompiled header setup
#include "stdafx.h"

// it's too bad this can't really be first
// I'm guessing "This" refers to the corresponding header
#include "This.h"

// C stdlib then C++ stdlib is what I usually do,
// whether the C headers are spelled XXX.h or cXXX.
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <list>
#include <string>

// someone mentioned winsock2 needs to be before windows
#include <Winsock2.h>

#include <tchar.h>
#include <Windows.h>

#include <boost/regex.hpp>

#include "mystring_written_by_c.h"
#include "mystring_written_by_cpp.h"

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.

旧时浪漫 2024-10-21 21:19:14

这很大程度上取决于您如何需要将它们包括在内。如果没有一个依赖于另一个,那么您可以相对自由地使用自己的方案来包含(一般来说,如果另一个需要,最重要的头文件将被隐式包含)。

但请注意,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.

提赋 2024-10-21 21:19:14

头文件的放置顺序并不重要。这主要是一个选择问题。许多人首先放置系统标头(<> 之间的标头),然后放置私有标头,但这取决于您。

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.

如果没有 2024-10-21 21:19:14

标准库,然后是不在标准库中的系统头文件,然后是私有头文件。用空行分隔每个类别。评论任何需要解释的内容。

如果您有依赖项,请在依赖于它们的标头之前包含标头所依赖的标头 (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!).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文