简体   繁体   English

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

[英]What to put in precompiled header? (MSVC)

What are the best candidates for a precompiled header file? 预编译头文件的最佳候选者是什么? Can I put STL and Boost headers there, even though they have templates? 我可以在那里放置STL和Boost标头,即使它们有模板吗? And will that reduce compile times? 这会减少编译时间吗? Also, what are the best IDE settings to reduce compile times? 另外,减少编译时间的最佳IDE设置是什么?

The quick answer: the STL and Boost headers do indeed belong in the precompiled header file, even though these header files define template classes. 快速回答:STL和Boost标头确实属于预编译头文件,即使这些头文件定义了模板类。

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. 生成预编译头文件时,编译器会解析头文本(一项重要任务!),并将其转换为二进制格式,该格式针对编译器的优势进行了优化。

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文件时将实例化模板类,它们也将从预编译头中的信息实例化,这对于编译器来说要快得多。


(later addition) (后来补充)

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. 应该在预编译的头包括有一件事是属于您的项目的一部分,并且需要经常更换,即使每一个.cpp文件中包括这些文件的文件。

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. 原因是这样 - 预编译头的生成可能需要很长时间,因为boost,stl和windows库非常大。

You might have a simple file (eg "StringDefs.h") that everything uses. 你可能有一个简单的文件(例如“StringDefs.h”),一切都使用。 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. 如果StringDefs.h包含在stdafx.h中,并且一个开发人员接触StringDefs.h,那么每个开发人员都必须等到整个预编译头重新编译。 It would be much faster if StringDefs.h was left out of the precompiled header, and parsed along with each .CPP file. 如果StringDefs.h被排除在预编译头之外,并且与每个.CPP文件一起解析,那将会快得多。

One addition to Andrew Shepherd's answer. Andrew Shepherd的答案之一。 Use the precompiled header for header files that are external to your project, for files that change infrequently. 对于项目外部的头文件,使用预编译头,用于不经常更改的文件。 If you're changing the header files in the current project all the time, it's probably not worth precompiling them. 如果您一直在更改当前项目中的头文件,则可能不值得对它们进行预编译。

I've written an article on techniques that reduce the compilation time. 我写过一篇关于减少编译时间的技术的文章。 Among these techniques a post on precompiled header and its application can be found here . 在这些技术中,可以在此处找到预编译头文件及其应用程序的帖子。 It also has a section on best practices that you may find interesting. 它还有一个您可能感兴趣的最佳实践部分。 CMake scripts that handle it transparently are included. 包含透明处理它的CMake脚本。

Put anything in the precompiled header that most of the .cpp files in that project would include anyway. 将任何内容放入预编译的头文件中,无论如何该项目中的大多数.cpp文件都会包含在内。 This goes for any header file, really. 这适用于任何头文件,真的。 This allows the compiler to parse these files once, and then reuse that information in all .cpp files in the same project. 这允许编译器解析这些文件一次,然后在同一项目的所有.cpp文件中重用该信息。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM