c++ - 预编译头如何减少编译时间

标签 c++ compiler-construction compilation precompiled-headers

我使用预编译 header 已有一段时间,并被告知(并看到)它们如何减少编译时间。但我真的很想知道发生了什么(幕后),这样可以加快我的编译速度。

因为据我所知,在 .cpp 中添加未使用的 include 会减慢编译时间,并且头文件可能包含大量未使用的 .cpp 头文件。

那么预编译头如何让我的编译速度更快呢?

最佳答案

来自 http://gamesfromwithin.com/the-care-and-feeding-of-pre-compiled-headers谢谢 (@Pablo)

A C++ compiler operates on one compilation unit (cpp file) at the time. For each file, it applies the pre-preprocessor (which takes care of doing all the includes and “baking” them into the cpp file itself), and then it compiles the module itself. Move on to the next cpp file, rinse and repeat. Clearly, if several files include the same set of expensive header files (large and/or including many other header files in turn), the compiler will be doing a lot of duplicated effort.

The simplest way to think of pre-compiled headers is as a cache for header files. The compiler can analyze a set of headers once, compile them, and then have the results ready for any module that needs them.

关于c++ - 预编译头如何减少编译时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291550/

相关文章:

c++ - ODBC Execute/Fetch of SQL 2005 存储过程结果集不能使用表@variable

c++ - 在旧版本的 linux 上运行我的代码

c++ - 在 C++ 中标记类/方法已过时或弃用

C 解释器(不是编译器)?

c++ - 我们可以在 Windows 上使用/模拟 Solaris/Aix C/C++ 编译器吗?

c++ - 没有对象就不能调用成员函数 = C++

c++ - 代码更改不生效

c++ - C++:检测 vector 在函数中是否为<complex>类型

c++ - 窗口没有响应 XMoveResizeWindow 请求

c++ - 如何将值传递给 lambda 函数