Qt最佳实践包括头文件和预编译头文件? [英] Best Practice For Qt Includes And Precompiled Headers?

查看:325
本文介绍了Qt最佳实践包括头文件和预编译头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用现代的和最新的C ++编译器(例如MSVC2015)并激活预编译的标头时,关于包含Qt标头文件的当前最佳实践"是什么?

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?

示例:

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

      新项目更常见吗?

      推荐答案

      据我所知,Qt include和预编译头没有特定的限制/推荐/优势/缺点.当包含第三方头文件(Qt或boost或其他文件)时,将应用相同的规则.

      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.

      通常(对于Qt包括,为true,但对于任何其他第三方包括(例如,STL,甚至包括您自己的代码,也是如此)),您应使包含的内容最少.您包含的文件越少,编译速度越快.包括您实际上不需要的文件将使编译速度变慢.此外,如果您包含但不使用的此类头文件被编辑/修改(通常,第三方头文件不应该如此),则包括该文件在内的任何文件都将需要重新编译,即使没有编译也是如此.真的使用其中包含的代码....

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

      因此,一般规则是仅包括您真正需要的文件.因此,如果您的文件只需要QStringList,则最好包括<QtCore/QStringList>而不是<QtCore>.

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

      如果您担心编译时间,请确保仅在必要时包括头文件(.h)中的文件,如果可以使用前向声明,请使用它,并且仅包括实现中的必要头文件(.cpp ).修改头文件后,这将大大减少项目的编译时间(阅读此).

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

      现在,如果您的项目有很多文件,其中包括一些Qt文件,则可以使用预编译头文件来优化编译.这些文件将仅被编译一次.但是,由于所有文件最终都将使用相同的预编译头文件(其本身包括许多头文件),因此仅在以下情况下才应这样做:

      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:

      • 预编译的头文件应主要是第三方头文件,因此它们不应更改.因为如果更改,则将需要重新编译所有文件...
      • 编译器必须支持预编译的头文件(否则,编译可能会起作用,但最终可能会变慢,因为每个文件最终都将包含项目的所有预编译的头文件……因此可能比实际需要的文件更多). /li>
      • 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).

      这篇关于Qt最佳实践包括头文件和预编译头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆