简体   繁体   English

预编译头设计问题

[英]Pre-Compiled Header Design Question

I have code that uses a pre-compiled header. 我有使用预编译头的代码。 (previously done by someone else) (之前由其他人完成)

In it, they are including several .h files. 在其中,它们包括几个.h文件。

If I have classes that use common .h files that are not currently in the existing pre-compiled header, would tossing them in there be of any real benefit? 如果我有使用普通.h文件的类,这些文件当前不在现有的预编译头文件中,那么将它们扔在那里有什么好处呢? Maybe compilation speed, but I was thinking it would clean up the classes/headers a bit too? 也许编译速度,但我认为它会清理类/标题有点过吗?

What are do's and don't with pre-compiled headers? 对预编译的标题有什么作用和不做什么?

DO NOT rely on headers being included by your precompiled header for "code cleanup" by removing those headers from your other source files. 通过从其他源文件中删除这些标头, 不要依赖预编译标头包含的标头进行“代码清理”。 This creates a nightmare if you ever want to stop using PCH. 如果你想停止使用PCH,这会造成噩梦。 You always want your dependencies to be explicit in every source file. 您始终希望您的依赖项在每个源文件中都是显式的。 Just include them in both places -- there is no harm in it (assuming you have appropriate include guards in place). 只需将它们包含在两个地方 - 它就没有任何危害(假设您有适当的包括防护装置)。

A header file that is included by multiple source files is a good candidate for inclusion in the PCH (particularly if it is lengthy). 多个源文件包含的头文件是包含在PCH中的良好候选者(特别是如果它很长)。 I find that I don't take the advice too seriously to only put headers that rarely change into the PCH. 我发现我并没有太认真地接受这个建议, 只能很少改变的标题放到PCH中。 But, this depends on your overall project structure. 但是,这取决于您的整体项目结构。 If you frequently do full builds, definitely avoid this advice. 如果您经常进行完整构建,请务必避免此建议。 If you want to minimize the work in incremental rebuilds, then it's a consideration. 如果要最小化增量重建中的工作,那么这是一个考虑因素。 In my experience, rebuilding the PCH is relatively fast, and the cost of this is far outweighed by the overall speedup of compilation in general (in most cases). 根据我的经验,重建PCH的速度相对较快,而且总体编译速度(大多数情况下)远远超过了编译成本。 I'm not sure if all PCH systems are smart enough to figure out that every source file does not need to be rebuilt when a header included in the PCH changes (VC++ is), but explictly #include ing everything you need in every translation unit will surely facilitate this (another reason you should not rely on what is included by your PCH) 我不确定所有PCH系统是否足够智能,以确定当PCH中包含的标题发生变化(VC ++)时,不需要重建每个源文件,但是明确地#include每个翻译单元所需的一切必将促进这一(另一个原因是什么你PCH包括你应该依赖)

If your compiler supports an option to show the #include tree for each file during compilation, this can be a great help to identify headers that should be included in the PCH (the ones that show up the most). 如果您的编译器支持在编译期间为每个文件显示#include树的选项,这对于识别应包含在PCH中的标题(显示最多的标题)非常有帮助。 I recently went through this on a project I'm working on (which was already using PCH, but not optimally) and sped up the build of 750K lines of C++ from roughly 1.5 hours to 15 minutes. 我最近在一个正在研究的项目(已经使用PCH,但不是最佳的)上完成了这项工作,并加快了750K系列C ++的构建,从大约1.5小时到15分钟。

Put non-changing system includes into the precompiled header. 将不变系统包含到预编译头中。 That will speed up compilation. 这将加快编译速度。 Don't put any of your own header files that you might change into the precompiled header, because each time you change them you will have to rebuild the entire precompiled header. 不要将您可能更改的任何自己的头文件放入预编译头中,因为每次更改它们时都必须重建整个预编译头。

It is a trade-off: system/library headers definitely go in the PCH, for ones in your project it depends. 这是一个权衡:系统/库标题肯定会出现在PCH中,因为它取决于您的项目中的标题。

Our project has a large amount of generated code that is changed much less frequently that other parts of the project. 我们的项目有大量生成的代码,其更改频率远低于项目的其他部分。 These headers go in the PCH because they take a lot of time to process in each individual file. 这些标题放在PCH中,因为它们需要花费大量时间来处理每个单独的文件。 If you change them it is expensive, but you have to weigh that cost against the more frequent smaller savings of having them in the file. 如果你改变它们是昂贵的,但是你必须权衡这个成本与在文件中使用它们的更频繁的较小节省。

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

相关问题 在每个make上都会创建预编译的标头? - Pre-compiled header gets created on every make? 包含标准库时不使用预编译头文件 - Pre-compiled header is not used when including standard library 为什么这些标头仅在预编译的标头之外起作用? - Why do these headers only work outside of the pre-compiled header? C ++预编译头和包含的文件组织 - C++ Pre-compiled header and included file organization 调用预编译的可执行文件 - calling pre-compiled executables 预处理器#defines以及如何在没有预编译头的情况下全局使用? - pre-processor #defines and how to use globally without a pre-compiled header? 如何通过预处理器指令检查programmatic是否在Visual C ++中需要预编译头? - How to check programmatic by preprocessor directive whether a pre-compiled header is required in Visual C++? C:Visual Studio和Eclipse + MinGW中的预编译头文件 - C : Pre-compiled header file in Visual Studio and Eclipse+MinGW 库项目的预编译头文件中定义的变量和函数是否可供使用该库的应用程序使用? - Are variables and functions defined in a library project's Pre-compiled header available to applications using the library? 禁用在clang前端中查找预编译的头文件 - Disable looking for pre-compiled header file in clang front-end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM