Results for precompiled header
On this page:
 
Computer Desktop Encyclopedia:

precompiled header

A C header file that has been pre-parsed and converted into an intermediate language that is faster to process by the compiler. See header file.



 
 
Wikipedia: precompiled header

[[Category:C++ articles needing expert attention]]

In computer programming, a Precompiled header is a performance feature found in some C or [[C++]] compilers used to reduce compilation time.

Overview

In order to execute a program written in a language such as C, the source code must first be converted from readable plain text into a binary file that can be run by the computer's processor. This process is called compilation and can be a lengthy one. Commonly, when programming complex applications, use is made of header files, which are sections of code included in many other programs for performing common tasks. In order to speed up compilation, these seldom changed files can be converted into an intermediate form, which is easier for the compiler to understand (typically a dump of the parse tree) so that subsequent compilations are faster. This intermediate form is called precompiled header files, for which .PCH or .GCH extensions are typical though they may vary according to the compiler.

While this kind of header can be advantageous, there is a chance for slowing down compilation if too much unused code is contained in the headers. This can be overcome by using a larger number of smaller headers and not including those for functions that are unused. One can further break lots of dependencies by using forward declarations and Pimpl idiom.

The results of subdividing the source code are often far better [citation needed] than one could possibly achieve with precompiled headers. However, the compiler can not automatically handle its reorganization.

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 precompiled header feature turned on, the compiler will generate a precompiled 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 precompiled headers. We can use the easier syntax in B1.h and advise the compiler to use precompiled 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 precompiled header A.pch. As the file B1.h is not changed, it does not need to be parsed again and the already existing precompiled header B.pch can be used.

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 precompile 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 precompiled headers, as well as they may specify an alternative name (though stdafx.h is used by default).

GCC

Precompiled headers are supported in GCC (3.4 and newer). GCC's approach is similar to these of VC and compatible compilers. GCC saves the precompiled version using a ".gch" suffix. When compiling source files, the compiler checks whether those files are present. If possible, the precompiled 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 precompiled 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 precompiled 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

External links

[[Category:C++]]


 
 

Join the WikiAnswers Q&A community. Post a question or answer questions about "precompiled header" at WikiAnswers.

 

Copyrights:

Computer Desktop Encyclopedia. THIS COPYRIGHTED DEFINITION IS FOR PERSONAL USE ONLY.
All other reproduction is strictly prohibited without permission from the publisher.
© 1981-2008 Computer Language Company Inc.  All rights reserved.  Read more
Wikipedia. This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Precompiled header" Read more

Search for answers directly from your browser with the FREE Answers.com Toolbar!  
Click here to download now. 

Get Answers your way! Check out all our free tools and products.

On this page:   E-mail   print Print  Link  

 

Keep Reading

Mentioned In: