Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What Is the difference between pre-compiled headers and pre-compiled binaries

Tags:

c++

Wiki reference mentions that some headers can sometime contain large amount of source code and hence having them as pre compiled headers will save compilation time. https://en.wikipedia.org/wiki/Precompiled_header

if precompiled header can contain compiled source code then how is it different from pre compiled binaries.

like image 957
bharath Avatar asked Jan 11 '17 06:01

bharath


People also ask

What is pre compiled binaries?

Precompiling a BinaryOn the Cloud Workstation, the user can download the SAMtools source code and compile it in the worker environment, ensuring that the binary will run on future workers.

Should I use precompiled headers?

Usage of precompiled headers may significantly reduce compilation time, especially when applied to large header files, header files that include many other header files, or header files that are included in many translation units.

What is the purpose of precompiled header?

Precompiled headers (PCH) are a performance feature supported by some compilers to compile a stable body of code, and store the compiled state of the code in a binary file. During subsequent compilations, the compiler will load the stored state, and continue compiling the specified file.

Can header files be compiled?

Only source files are passed to the compiler (to preprocess and compile it). Header files aren't passed to the compiler. Instead, they are included from source files.


2 Answers

C++ defines the notion of a "translation unit". A translation unit is an anchor point where translation starts, and a program typically comprises several such translation units. Which units are passed to the compiler as "translation unit" actually depends on the settings in your IDE, on makefiles, and other configurations. But informally most configurations take your .cpp and .c- files as being translation units.

To make it easier, we can think of a translation unit as something of which a compiler makes a binary, and a linker combines several binaries then to a program.

So header files are typically not configured as being translation units, and they typically do not yield a binary on their own, even if they contain source code. They are rather thought of being imported from translation units and will be compiled together with them.

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

However, from the content and the type of source code in it, a header file is not distinguishable from other source code files. You may pass a "header file" to the compiler as translation unit, and the compiler will actually make a "normal" binary out of it. It's really just that they are typically not declared as a root for compilation.

Hope that helps.

like image 106
Stephan Lechner Avatar answered Sep 27 '22 21:09

Stephan Lechner


The main difference is the more of information stored in a precompiled header compared to a compiled source. If you compile a source to a binary information about defines and templates get lost. Defines vanish to their values and templates get only in instantiated form in to the binary. A precompiled header keeps the information about the generic form of templates and the name and values of defines as they are needed, if the precompiled header is included somewhere. For pure source precompiled header are identical to compiled source.

like image 35
user6556709 Avatar answered Sep 27 '22 22:09

user6556709