包含 Qt 标头的正确方法是什么?

发布于 2024-10-08 00:02:52 字数 731 浏览 0 评论 0原文

到目前为止,我知道 #include Qt 类的几种方法:

  • #include

    这会带来特定模块的所有类,例如 QDomDocumentQDomElementQDomNode 以及来自 #include < 的许多其他类;QtXml>.

  • #include

    这添加了人们可能想要使用的特定类的声明,例如QEventQStringListQFile

  • #include

    这与前一个方法具有相同的效果,除了标头名称不同之外。

所以我想知道,还有其他方法来 #include Qt 类吗?它们是否相等,或者由于某些原因其中一些比其他更受青睐?这是否取决于 .cpp.h 文件中的 #include-ing?这会影响可执行文件的编译速度和大小吗?

简而言之,最好的方法是什么?

So far I know several ways to #include Qt classes:

  • #include <QtModule>

    This brings all classes of a specific module, for example QDomDocument, QDomElement, QDomNode and numerous others from #include <QtXml>.

  • #include <QClassName>

    This adds declaration of a specific class one may want to use, e.g. QEvent, QStringList, QFile.

  • #include <qcstyleheader.h>

    This has effect of the previous method, except for differently looking header name.

So I wonder, are there any other ways to #include Qt classes? Are they equivalent, or are some of them preferred to others because of some reasons? Does this depend on #include-ing inside a .cpp or a .h file? Does this influence compilation speed and size of the executable?

In short, what is the best way to go?

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

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

发布评论

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

评论(4

糖果控 2024-10-15 00:02:52

通常,头文件越多,编译器解析每个模块所需的时间就越长。 (显然,预编译标头使得这些内容变得毫无意义。)因此,您通常希望包含正确构建应用程序所需的最少标头文件。

如果您仅在给定的编译单元中使用几个类,则只需以现代风格按名称包含这些类:

#include <QEvent>
#include <QPainter>
#include <QFont>

如果您使用给定模块中的大量类,则包含该模块可能同样容易-level header,例如:

#include <QtGui>

如果不存在较新的样式标头,通常只使用较旧的 .h 样式。

现在,预编译头通过编译成二进制形式一次来缓解许多这些问题。但是将预编译符号加载到内存中并在编译时搜索它们仍然存在成本。因此,投入越少,构建的效率就越高。

As a rule, the more header files there are, the longer it takes the compiler to parse each module. (Obviously precompiled headers render some of this moot.) So you generally want to include the fewest header files necessary to build your app correctly.

If you are only using a few classes in a given compilation unit, then just include the classes by name, in the modern style:

#include <QEvent>
#include <QPainter>
#include <QFont>

If you use a large number of classes from a given module, it is probably just as easy to include the module-level header, such as:

#include <QtGui>

You generally only use the older .h style if a newer style header doesn't exist.

Now precompiled headers mitigate many of these issues by compiling once into a binary form. But there is still a cost to loading the precompiled symbols into memory and searching through them at compilation time. So the less you put in, the more efficient the build will be.

情仇皆在手 2024-10-15 00:02:52

这似乎是一个普遍的包含问题。答案很简单:只包含您必须包含的内容。否则你会减慢编译速度。在标头中,尝试转发声明。如果这还不够,请包含声明该类的标头。

This seams to be a general include question. And the answer is simple : include only what you must. Otherwise you are slowing down the compilation. In the header, try to forward declare. If that is not enough, then include the header declaring the class.

鼻尖触碰 2024-10-15 00:02:52

我认为没有任何普遍正确的方法来包含...我首选的方法是第二种,因为它是最简单的 - 您只需为文件中使用的每个类执行此操作,很多需要更少的思考。

至于编译速度,是的,Qt 头文件的编译需要一段时间。如果您想更快地编译,请在 .cpp 文件中 #include 您需要的所有内容。有时您需要包含在 .h 文件中 - 如果声明一个类,则始终需要 #include 其基类,以及您在类内按值保留或在类函数中按值传递的任何成员的类。但是,对于那些声明为指针、智能指针或引用的成员和函数参数,您可以将 #include 替换为“class foo;”宣言。然后你的标头会编译得更快,但每当你实际按值使用它们时,你仍然需要 #include 这些类。

I don't think there is any universally right way of including... My preferred way is the second one, since it's easiest - you just do it for every class you use in your file, a lot less thinking is needed.

As for the compilation speed, yes, Qt headers happen to take a while to compile. If you want to compile faster, #include everything you need in .cpp files. Sometimes you need to include in .h files - if you declare a class, you always need to #include its base class, and classes of any members that you keep by value inside class or pass by value in class functions. However, for those members and function parameters that are declared as pointers, smart pointers or references, you may substitute #include with "class foo;" declaration. Then your header will compile faster, but you still need to #include these classes whenever you actually use them by value.

浅语花开 2024-10-15 00:02:52

就我个人而言,我发现将我在预编译头中使用的所有 Qt 包含在内有显着的好处(编译时间缩短约 30%?)(示例),而不是根据需要在每个 .h/.cpp 中。当然,较小的缺点是,您可能会忽略您的各个源文件具体依赖于 Qt 的哪些部分,但我自己还没有发现这是一个问题。

Personally I've found significant benefits (~30% off compile times?) from including all the Qt includes I use in a precompiled header (example), rather than in each .h/.cpp as needed. Of course the minor downside is, you can lose sight of which bits of Qt your individual source files specifically depend on, but I haven't found this to be a problem myself.

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