包含头文件的更好方法是什么? #include<>后跟#include“”或其他?
可能的重复:
#include和 #include 之间有什么区别?和#include“文件名”
C/C++ 包含文件顺序/最佳实践
include 语句应按什么顺序在 C++ 中的头文件和源文件中? #包括<>后面跟着 #include "" 还是其他?
另外,源文件的头文件是否应该位于源文件中的所有包含语句之前?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我更喜欢按以下顺序包含:
I prefer to include in this order:
一般的经验法则是按顺序包含标头,以便最大限度地检测到您自己的标头之一未能包含其所需的所有标头的机会。即首先包括它。但由于不可能对您包含的所有标头都执行此操作,因此这只是一种模糊的指南,不会造成伤害并且可能会带来一些好处。
当您有很多标题时,请尝试更加系统化。
例如,按照它们所实现的目标(例如 [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.,
没有更好或更坏,它们服务于不同的目的。
#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.只要遵循项目的现有约定(如果有任何
#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.这与是否将左大括号放在自己的行上一样重要。我建议你选择你更喜欢的一个,并保持一致。
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.