How a precompiled header reduces compilation time

I used the precompiled header for a while, and I was told (and seen) how they can reduce compilation time. But I would really like to know what is happening (under the hood) so that it can do my compilation faster.

Due to what I know, adding an unused include to .cpp can slow down compilation time, and the header file may contain a lot of unused header in .cpp.

So how does a precompiled header make my compilation faster?

+4
source share
4 answers

From http://gamesfromwithin.com/the-care-and-feeding-of-pre-compiled-headers Thanks (@Pablo)

The C ++ compiler works with one compilation module (cpp file) for a while. For each file, a preprocessor is used (which takes care of doing all the incoming and β€œbaking” them into the cpp file itself), and then it compiles the module itself. Go to the next cpp file, rinse and repeat. It is clear that if several files contain the same set of expensive header files (large and / or containing many other header files in turn), the compiler will do a lot of duplication of effort.

The easiest way to think about precompiled headers is to cache the header files. The compiler can analyze a set of headers once, compile them, and then prepare the results for any module that they need.

+4
source

Basically, the header file is compiled once for each translation unit ( .cpp file) with which it is included. Using a precompiled header header saves the time used to compile the include file again and again. This is really useful when the header file to be precompiled is very large (or indirectly includes many other header files).

+2
source

Many years ago, I had access to a C compiler that printed out the number of processed lines (Watcom C version 6 or so). Compiling files with less than 100 lines of C code displays the number of 5000 or even 10000 lines. All of them were included. In other words, #included code completely dominates at compile time. Therefore, all you can do to reduce this will be useful. You yourself can see the compilers that allow you to disable preprocessing: compare the time for complete system assemblies with it and without it.

+1
source

I think that "precompiled" says something about how to make compilation faster. You can read about the basic concept that I think of:

http://en.wikipedia.org/wiki/Precompiled_header

0
source

Source: https://habr.com/ru/post/1383368/


All Articles