在预编译头文件中放什么?(MSVC)

2021-12-22 00:00:00 visual-c++ c++ precompiled-headers

预编译头文件的最佳候选者是什么?我可以将 STL 和 Boost 标头放在那里吗,即使它们有模板?这会减少编译时间吗?另外,减少编译时间的最佳 IDE 设置是什么?

What are the best candidates for a precompiled header file? Can I put STL and Boost headers there, even though they have templates? And will that reduce compile times? Also, what are the best IDE settings to reduce compile times?

推荐答案

快速回答:STL 和 Boost 头文件确实属于预编译头文件,即使这些头文件定义了模板类.

The quick answer: the STL and Boost headers do indeed belong in the precompiled header file, even though these header files define template classes.

在生成预编译头文件时,编译器会解析头文本(一项重要任务!),并将其转换为针对编译器进行优化的二进制格式.

When generating a precompiled header file, a compiler parses the header text (a significant task!), and converts it into a binary format that is optimised for the compiler's benefit.

即使在编译其他 .cpp 文件时会实例化模板类,它们也会根据预编译头文件中的信息进行实例化,这对于编译器来说读取速度要快得多.

Even though the template classes will be instantiated when other .cpp files are compiled, they will be instantiated from information in the precompiled header, which is significantly faster for the compiler to read.


(后来添加)

您应该不在预编译头文件中包含的一件事是作为您项目的一部分并且经常更改的文件,即使每个单独的 .CPP 文件都包含这些文件.

One thing that you should not include in a precompiled header are files that are part of your project and are changed frequently, even if every single .CPP file includes these files.

原因是这样 - 预编译头的生成可能需要很长时间,因为 boost、stl 和 windows 库非常大.

The reason is this - the generation of the precompiled header can take a long time, because the boost, stl and windows libraries are very large.

您可能有一个所有东西都使用的简单文件(例如StringDefs.h").如果 StringDefs.h 包含在 stdafx.h 中,并且一个开发人员接触了 StringDefs.h,那么每个开发人员都必须等到 整个预编译头重新编译.如果将 StringDefs.h 排除在预编译头之外,并与每个 .CPP 文件一起解析,速度会快得多.

You might have a simple file (eg "StringDefs.h") that everything uses. If StringDefs.h is included in stdafx.h, and one developer touches StringDefs.h, then every developer has to wait until the entire precompiled header recompiles. It would be much faster if StringDefs.h was left out of the precompiled header, and parsed along with each .CPP file.

相关文章