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?
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.
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.
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.
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.