包含头文件的更好方法是什么? #include<>后跟#include“”或其他?

发布于 2024-10-12 21:18:10 字数 516 浏览 3 评论 0 原文

可能的重复:
#include 和 #include 之间有什么区别?和#include“文件名”
C/C++ 包含文件顺序/最佳实践

include 语句应按什么顺序在 C++ 中的头文件和源文件中? #包括<>后面跟着 #include "" 还是其他?

另外,源文件的头文件是否应该位于源文件中的所有包含语句之前?

Possible Duplicates:
what is the difference between #include <filename> and #include “filename”
C/C++ include file order/best practices

In what order should the include statements in a header file and source file come in C++? #include <> followed by #include "" or the otherwise?

Also, should the header file of a source file precede all include statements in source file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

心奴独伤 2024-10-19 21:18:10

我更喜欢按以下顺序包含:

  • 首先是标准库。
  • 然后是第三方库。
  • 最后是我自己写的标题。

I prefer to include in this order:

  • Standard libraries first.
  • Then third-party libraries.
  • Lastly, headers that I have written myself.
南烟 2024-10-19 21:18:10

一般的经验法则是按顺序包含标头,以便最大限度地检测到您自己的标头之一未能包含其所需的所有标头的机会。即首先包括它。但由于不可能对您包含的所有标头都执行此操作,因此这只是一种模糊的指南,不会造成伤害并且可能会带来一些好处。

当您有很多标题时,请尝试更加系统化。

例如,按照它们所实现的目标(例如 [windows.h] 后跟一些需要 [windows.h] 的 MS 标头)和/或按字母顺序对它们进行分组。

最后,不要在这上面花太多时间。 :-)

干杯&呵呵,

A general rule of thumb is to include headers in an order so as to maximize the chance of detecting that one of your own headers fails to itself include all that it needs. I.e. include that first. But since it's impossible to do that for all headers that you include, this is just a kind of vague guideline that doesn't hurt and might do some good.

When you have many headers, try to be a bit more systematic.

Like, group them by what they achieve (like [windows.h] followed by some MS header that requires [windows.h]), and/or alphabetically.

In the end, just don't use too much time on this. :-)

Cheers & hth.,

演多会厌 2024-10-19 21:18:10

没有更好或更坏,它们服务于不同的目的。 #ncude "" 应该用于项目中的文件或未在系统范围内安装的直接依赖项。其中 #include <> 代表位于 /usr/include 或 simialr 文件夹中的包含文件(例如在 Linux 下),也称为系统库。

There is no better or worse, they server different purposes. #ncude "" is supposed to be used for files in your project or direct dependencies that are not system wide installed. Where #include <> are for inludes that (eg under Linux) are located in your /usr/include or simialr folder, also called system libraries.

何时共饮酒 2024-10-19 21:18:10

只要遵循项目的现有约定(如果有任何 #include 指令)。如果没有,只要你保持一致,你做什么并不重要。

Just follow the project's existing conventions, if it has any for #include directives. If it doesn't, it doesn't really matter what you do as long as you're consistent.

东风软 2024-10-19 21:18:10

这与是否将左大括号放在自己的行上一样重要。我建议你选择你更喜欢的一个,并保持一致。

This matters about as much as whether you put opening curly braces on their own line. I would suggest that you pick whichever one you like better, and be consistent.

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