Precompiled header

Precompiled header

In computer programming, a pre-compiled header is a technique used by some C or C++ compilers to reduce compilation time.

Overview

In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the compiler, usually specified by the use of compiler directives in the source file.

Header files can sometimes contain a very large amount of source code (for instance, the header files windows.h and Cocoa/Cocoa.h on Microsoft Windows and Mac OS X, respectively).

To reduce compilation times some compilers allow header files to be precompiled into a form that is faster for the compiler to process. This intermediate form is known as a "precompiled header", and is commonly held in a file named with the extensions .pch or .gch.

A related feature is the prefix header, which is a file that is automatically included by the compiler without requiring the use of any compiler directives. Prefix headers are commonly precompiled, but not all precompiled headers are prefix headers.

Usage

Simple Example

Given a C++ file source.cpp that includes header.hpp:

// header.hpp ...

// source.cpp #include "header.hpp" ...

When compiling source.cpp for the first time with the pre-compiled header feature turned on, the compiler will generate a pre-compiled header, header.pch. The next time, the compiler can skip the compilation phase relating to header.hpp and instead use header.pch directly.

Example with forward declaration

We have two classes A (in A.h and A.cpp) and B (in B.h and B.cpp). Class A is quite simple:

class A { ...};

When class B is composed by an attribute of class A, normally the class A will become part of the class:


#include "A.h"class B1 { ... A mA;};

To prevent recompilation of B1.h, when class A changes, sometimes pointer syntax will be used; B2.h:

class A;class B2 { ... A* mA;};

As you can see, a forward declaration of class A is used instead of including the header. But this pointer syntax complicates the usage of the variable as price for lesser compilation.

This problem can be solved by the usage of pre-compiled headers. We can use the easier syntax in B1.h and advise the compiler to use pre-compiled headers. Then the files A.pch and B1.pch will be created.

When the class A is changed now, only A.h has to be parsed again and written to the pre-compiled header A.pch. As the file B1.h is not changed, it does not need to be parsed again and the already existing pre-compiled header B1.pch can be used.

Common Implementations

stdafx.h

stdafx.h is a file, generated by Microsoft Visual Studio IDE wizards, that describes both standard system and project specific include files that are used frequently but hardly ever changed.

Compatible compilers (for example, Visual C++ 6.0 and newer) will pre-compile this file to reduce overall compile times. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled.

The AFX in stdafx.h stands for Application Framework eXtensions. AFX was the original abbreviation for the Microsoft Foundation Classes (MFC). Optionally, Visual Studio projects may avoid pre-compiled headers, as well as they may specify an alternative name (though stdafx.h is used by default).

GCC

Pre-compiled headers are supported in GCC (3.4 and newer). GCC's approach is similar to these of VC and compatible compilers. GCC saves the pre-compiled version using a ".gch" suffix. When compiling source files, the compiler checks whether those files are present. If possible, the pre-compiled version is used. If this is not possible, then, the normal header is still present as a fall-back plan.

GCC can only use the pre-compiled version if the same compiler switches are set as when the header was compiled and it may use at most one. Further, only preprocessor instructions may be placed before the pre-compiled header (because it must be directly or indirectly included through another normal header, before any compilable code).

GCC automatically identifies most header files using their extension. However, if this fails (e.g. because of non standard header extensions), the -x switch can be used to be sure that GCC treats the file as a header instead of a normal source file.

See also

*Single Compilation Unit
*Prefix header

External links

* [http://www.gamesfromwithin.com/articles/0504/000086.html The Care and Feeding of Pre-Compiled Headers]
* [http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html Precompiled Headers with GCC]
* [http://www.cygnus-software.com/papers/precompiledheaders.html Fractal eXtreme: Precompiled headers]


Wikimedia Foundation. 2010.

Игры ⚽ Поможем написать курсовую

Look at other dictionaries:

  • Prefix header — In computer programming, a prefix header is a feature found in some C or C++ compilers used to simplify code and/or to reduce compilation time. Overview In the C and C++ programming languages, a header file is a file whose text is automatically… …   Wikipedia

  • Liste der Dateiendungen/H — In dieser Liste sind übliche Dateinamenserweiterungen aufgelistet, die in einigen Betriebssystemen (wie zum Beispiel Microsoft Windows) zur Unterscheidung von Dateiformaten verwendet werden. In anderen Betriebssystemen erfolgt die… …   Deutsch Wikipedia

  • Список расширений имени файла/H — / * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Примечание: Поскольку расширение имени файла может быть любым, представленный список не является полным …   Википедия

  • Ctb — In dieser Liste sind übliche Dateinamenserweiterungen aufgelistet, die in einigen Betriebssystemen (wie zum Beispiel Microsoft Windows) zur Unterscheidung von Dateiformaten verwendet werden. In anderen Betriebssystemen erfolgt die… …   Deutsch Wikipedia

  • Liste der Dateiendungen/C — In dieser Liste sind übliche Dateinamenserweiterungen aufgelistet, die in einigen Betriebssystemen (wie zum Beispiel Microsoft Windows) zur Unterscheidung von Dateiformaten verwendet werden. In anderen Betriebssystemen erfolgt die… …   Deutsch Wikipedia

  • Liste von Dateinamenserweiterungen/C — In dieser Liste sind übliche Dateinamenserweiterungen aufgelistet, die in einigen Betriebssystemen zur Unterscheidung von Dateiformaten verwendet werden. In anderen Betriebssystemen erfolgt die Dateitypenidentifikation hauptsächlich über den… …   Deutsch Wikipedia

  • Список расширений имени файла/C — / * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Примечание: Поскольку расширение имени файла может быть любым, представленный список не является полным …   Википедия

  • Список расширений имени файла/P — / * A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Примечание: Поскольку расширение имени файла может быть любым, представленный список не является полным …   Википедия

  • PCH — Pacific Coast Highway (Governmental » Transportation) * Publishers Clearing House (Business » Firms) * Potlatch Corporation (Business » NYSE Symbols) * Pre Compiled Header (Computing » General) * Patch file (Computing » File Extensions) * Photon… …   Abbreviations dictionary

  • Microsoft Foundation Class Library — Developer(s) Microsoft Initial release 1992 Stable release 10.0.40219.1 [1] …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”