项目中的每个C ++标头都作为预编译的标头 [英] Every C++ header in a project as a precompiled header

查看:74
本文介绍了项目中的每个C ++标头都作为预编译的标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常的方法是在一个项目中包含一个最常见的包含项的预编译头文件.

The usual approach is to have one precompiled header in a project that contains the most common includes.

问题是,它太大或太大了.当它太小时,它不会覆盖所有已使用的标头,因此必须在每个模块中一遍又一遍地处理这些标头.当它太大时,由于以下两个原因会大大降低编译速度:

The problem is, that it is either too small or two big. When it is too small, it doesn't cover all the used headers so these have to be processed over and over in every module. When it is too large, it slows down the compilation too much for two reasons:

  1. 当您更改预编译标题中包含的标题中的某些内容时,该项目需要太频繁地重新编译.
  2. 预编译头太大,因此实际上将其包含在每个文件中会减慢编译速度.

如果我预先编译了项目中的所有头文件,该怎么办?这将添加一些额外的编译器工作以对其进行预编译,但随后它将非常好地工作,因为不必将标头处理两次(即使准备预编译的标头也将递归使用预编译的标头),因此无需放入任何其他内容模块,只有实际需要重新编译的模块才会被重新编译.换句话说,对于额外的O(N)复杂度,我将(理论上)优化C ++ include的O(n ^ 2)复杂度.O(N)的先兆,对预编译数据的处理仍为O(N ^ 2),但至少要最小化.

What if I made all of the header files in a project precompiled. This would add some additional compiler work to precompile them, but then it would work very nicely, as no header would have to be processed twice (even preparing the precompiled header would use precompiled headers recursively), no extra stuff would have to be put into modules and only modules that are actually needed to be recompiled would be recompiled. In other words, for extra work O(N) complexity I would (theoretically) optimise O(n^2) comlexity of C++ includes. The precosseor to O(N), the processing of precompiled data would still be O(N^2), but at least minimised.

有人尝试过吗?它可以增加现实生活中的编译时间吗?

Did anyone tried this? Can it boost compile times in real life scenarios?

推荐答案

我自己对带有预编译标头的GCC和Clang的经验是,每次编译只能提供一个预编译标头.另请参见 GCC文档,我引用:

My own experience with GCC and Clang with precompiled headers is that you only can give a single pre-compiled header per compilation. See also the GCC documentation, I quote:

仅当满足以下条件时,才可以使用预编译的头文件:

A precompiled header file can be used only when these conditions apply:

  • 在特定的编译中只能使用一个预编译的标头.
  • ...

在实践中,可以将每个标头编译为预编译的标头.(如果要验证是否包含所有内容,建议使用,如果要加快编译速度,则不建议使用)

In practice, it's possible to compile every header to a precompiled header. (Recommended if you want to verify if everything is included, not recommended if you want to speed up compilation)

根据您的代码,您可以根据需要编译的代码来决定使用其他预编译的标头.但是,通常,它是头文件的编译时间,CPP文件的编译时间与维护之间的平衡.

Based on your code, you can decide to use a different precompiled header based on the code that needs to be compiled. However, in general, it's a balancing act between compile time of the headers, compile-time of the CPP files and maintenance.

添加一个简单的预编译头文件,其中已经包含几个标准头文件,例如字符串,向量,映射,实用程序……已经可以以显着的百分比加快编译速度.(很久以前,我注意到一个小项目占15-20%)

Adding a simple precompiled header that already contains several standard headers like string, vector, map, utility ... can already speed up your compilation with a remarkable percentage. (A long time ago, I've noticed a 15-20% on a small project)

从预编译头文件中获得的主要收益是:

The main gain you get from precompiled headers is that it:

  • 只需读取1个文件即可读取更多文件,从而改善了磁盘访问权限
  • 读取为阅读而不是纯文本格式进行了优化的二进制格式
  • 它不需要进行所有错误检查,因为创建时已经完成了

即使添加了一些并非在各处都使用的标头,它仍然可以更快.

Even if you add a few headers that you don't use everywhere, it can still be much faster.

最近,我还找到了 Clang构建分析器,它不适用于大型项目(请参阅github上的问题),但是,它可以为您提供一些有关时间花在哪里以及可以改善什么的见解.(或者您可以在代码库中改进的地方)

Lately, I also found the Clang build analyzer, it ain't ideal for big projects (see issue on github), though, it can give you some insights on where the time is being spent and what it can improve. (Or what you can improve in the codebase)

公平地说,我目前不使用预编译的头文件.但是,我确实希望在我正在处理的项目中看到它已启用.

In all fairness, I don't use precompiled headers at this point in time. However, I do want to see it enabled on the project I'm working on.

其他一些有趣的读物:

这篇关于项目中的每个C ++标头都作为预编译的标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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