简体   繁体   English

项目中的每个 C++ 头文件都作为预编译头文件

[英]Every C++ header in a project as a precompiled header

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. The project needs to be recompiled too often when you change something in header contained in the precompiled header.当您更改预编译头文件中包含的头文件中的某些内容时,需要经常重新编译项目。
  2. The precompiled header is too large, so including it in every file actually slows down compilation.预编译头文件太大,因此将它包含在每个文件中实际上会减慢编译速度。

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.换句话说,对于额外的工作 O(N) 复杂度,我会(理论上)优化 C++ 包含的 O(n^2) 复杂度。 The precosseor to O(N), the processing of precompiled data would still be O(N^2), but at least minimised. O(N) 的 precosseor,预编译数据的处理仍然是 O(N^2),但至少最小化。

Did anyone tried this?有没有人试过这个? Can it boost compile times in real life scenarios?它可以提高现实生活场景中的编译时间吗?

With GCC , the reliable way to use precompiled headers is to have one single (big) header (which #include -s many standard headers ...), and perhaps include some small header after the precompiled one.对于GCC ,使用预编译头文件的可靠方法是拥有一个(大)头文件(其中#include -s 许多标准头文件...),并且可能在预编译头文件之后包含一些头文件。

See this answer for a more detailed explanation (for GCC specifically).请参阅此答案以获取更详细的解释(专门针对 GCC)。

My own experience with GCC and Clang with precompiled headers is that you only can give a single pre-compiled header per compilation.我自己对 GCC 和 Clang 使用预编译头文件的经验是,每次编译只能提供一个预编译头文件。 See also the GCC documentation , I quote:另请参阅GCC 文档,我引用:

A precompiled header file can be used only when these conditions apply:预编译头文件只有在满足以下条件时才能使用:

  • Only one precompiled header can be used in a particular compilation.在特定编译中只能使用一个预编译头。
  • ... ...

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) (如果要验证是否包含所有内容,建议使用,如果要加快编译速度,则不建议使用)

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.然而,一般来说,这是头文件的编译时间、CPP 文件的编译时间和维护之间的平衡行为。

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) (很久以前,我注意到一个小项目有 15-20%)

The main gain you get from precompiled headers is that it:您从预编译头文件中获得的主要好处是:

  • only have to read 1 file instead of more, which improves on disk access只需要读取 1 个文件而不是更多,这改进了磁盘访问
  • reads a binary format that's optimized for reading instead of plain text读取为阅读而优化的二进制格式,而不是纯文本
  • it doesn't need to do all of the error checking as this was already done on creation它不需要做所有的错误检查,因为这已经在创建时完成了

Even if you add a few headers that you don't use everywhere, it can still be much faster.即使您添加了一些不在任何地方使用的标头,它仍然可以更快。

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.最近,我还发现了Clang 构建分析器,它不是大型项目的理想选择(请参阅github 上的问题),但是,它可以让您了解时间花在哪里以及可以改进的地方。 (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.但是,我确实希望在我正在处理的项目中启用它。

Some other interesting reads:其他一些有趣的读物:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 禁用 C++ 预编译头文件 - C++ Precompiled Header Disabled “语法错误:缺少';' 在带有预编译标头的c ++项目中,在“ {””之前 - “syntax error : missing ';' before '{'” in c++ project with precompiled header VisualStudio 中 C++ 项目中预编译的 Header 未正确链接 - Precompiled Header within C++ Project in VisualStudio not Linking correctly C ++ Automake预编译头支持centos - c++ automake precompiled header support on centos 编译C ++项目中不使用预编译头文件的C文件? - Compile C files in C++ project which do not use precompiled header? 使用CMake的C ++项目的单个共享预编译头(多个库和可执行文件) - Single shared precompiled header for C++ project (multiple libraries and executables) using CMake Xcode 7中有关C ++中预编译标头和前向声明的错误 - Bug in Xcode 7 about precompiled header and forward declaration in C++ Visual C ++链接LNK2019错误与预编译的头 - Visual C++ Linking LNK2019 Error with a Precompiled Header C ++中是否有跨平台的预编译标头框架/方法? - Are there cross-platform precompiled header frameworks/methods in C++? CMake 使用 C 风格的预编译头文件代替 C++ 版本的预编译 header - CMake uses C-style precompiled headers instead of C++ version of the precompiled header
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM