What is the difference between precompiled headers and precompiled binaries

The Wiki link indicates that some headers may sometime contain a large amount of source code and, therefore, having them as precompiled headers will save compilation time. https://en.wikipedia.org/wiki/Precompiled_header

if a precompiled header can contain compiled source code, then how does it differ from precompiled binaries.

+6
source share
2 answers

C ++ defines the concept of "translation unit". The translation block is the anchor point where the translation begins, and the program, as a rule, includes several such translation units. What units are passed to the compiler, since the "translation unit" actually depends on the settings in your IDE, on make files and other configurations. But informally, most configurations accept your .cpp and .c files as translation units.

To make this easier, we can think of the translation block as someone from whom the compiler makes binary code, and the linker combines several binary files, and then into a program.

Thus, the header files are usually not configured as translation units, and they usually do not produce the binary on their own, even if they contain source code. Rather, they are considered to be imported from translation units and will be compiled with them.

If such header files contain a lot of source code that will be included in several translation units, it makes sense to (pre) compile them once and save as much information as possible in the intermediate result, so that collecting the actual translation unit is faster. This saves time, but the "intermediate binary" is something internal and not displayed.

However, from the content and type of source code in it, the header file is no different from other source code files. You can pass the “header file” to the compiler as a translation unit, and the compiler will actually make “normal” binary code out of it. It is true that they are usually not declared root for compilation.

Hope this helps.

+2
source

The main difference is that more information is stored in a precompiled header compared to a compiled source. If you compile the source into binary information about definitions and patterns, get lost. Defines values ​​equal to zero, and templates are received only in an instantiated form in a binary file. The precompiled header stores information about the general form of the templates, and the name and values ​​determine how necessary they are if the precompiled header is included somewhere. For a precompiled header with a clean source, they are identical to the compiled source.

+1
source

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


All Articles