问题
What's the current "best practice" regarding the inclusion of Qt header files when using a modern and recent C++ compiler (e.g. MSVC2015) with precompiled headers activated?
Example:
#include <QStringList>
#include <QTreeWidget>
#include <QListWidget>
vs.
#include <QtCore>
#include <QtGui>
What convention should I choose for a new project?
What are the benefits/drawbacks of either?
What is more common for new projects?
回答1:
As far as I know, there is no specific limitation/recommendation/advantages/drawback with Qt includes and precompiled headers. When including third party header files (Qt or boost or whatever), the same rules apply.
In general (true for Qt includes, but also for any other third party includes, like STL for instance, and even when including your own code), you should make your includes minimal. The fewer files you include, the faster compilation will be. Including files you don't actually need will make compilation slower. Additionally, if such a header file you included but do not use, is edited/modified (which should not be the case for third parties header files, in general), any file including it will require to be recompiled, even if it does not really use the code included....
So the general rule is to only include files that you really need. So if your file only needs QStringList
, prefer including <QtCore/QStringList>
rather than <QtCore>
.
If you are concerned about compilation time, also make sure you only include files from header files (.h) if necessary, if forward declaration can be used, use it and only include the necessary header file from your implementation (.cpp). This will drastically reduce compilation time of your project when a header file gets modified (read this).
Now, if your project has many files that includes some Qt files, you may use precompiled header to optimize compilation. Those files will then be compiled once and only once. However, as all your files will end up using the same precompiled header (itself including many many header files), you should only do this if:
- The precompiled header files should mainly be third party header files, so that they are not meant to change. Because if on changes, then all your files will require to be recompiled...
- The compiler must support precompiled header (else, compilation may work but may also end up being slower because every file will end up including all the precompiled header of the project...so probably more files than it actually needs).
回答2:
I use next in all projects (for precompiled header):
#pragma once
#ifdef QT_CORE_LIB
# include <QtCore>
#endif
#ifdef QT_CONCURRENT_LIB
# include <QtConcurrent>
#endif
#ifdef QT_GUI_LIB
# include <QtGui>
#endif
#ifdef QT_WIDGETS_LIB
# include <QtWidgets>
#endif
#ifdef QT_MULTIMEDIA_LIB
# include <QtMultimedia>
#endif
#ifdef QT_NETWORK_LIB
# include <QtNetwork>
#endif
#ifdef QT_XML_LIB
# include <QtXml>
#endif
#ifdef QT_QML_LIB
# include <QtQml>
#endif
#ifdef QT_QUICK_LIB
# include <QtQuick>
#endif
#ifdef QT_SQL_LIB
# include <QtSql>
#endif
#ifdef QT_PRINTSUPPORT_LIB
# include <QtPrintSupport>
#endif
It will include necessary definitions only if you connect a module. Works well with msvs+qt addin or with cmake based projects (I use cotire).
回答3:
Including less headers reduces compilation time, and only that, so if you do
#include <QtCore/QStringList>
compilation it's slightly faster than if you did
#include <QtCore>
If you are sure that you depend on everything in QtCore
, include it, if not, include each header separately.
来源:https://stackoverflow.com/questions/46705043/best-practice-for-qt-includes-and-precompiled-headers