How to speed up compilation (and linking) using pre-compiled headers.

Abstract: How to speed up compilation (and linking) using pre-compiled headers.

  • Product Name: Borland C++ Builder
  • Product Version: 6.0 - 2006
  • Product Component: Compiler
  • Platform/OS Version: Win32

Description:

How to speed up compilation using pre-compiled headers.


Answer/Solution:

The steps set up your headers for pre-compilation:

1) Not all headers can be put in a pre-compiled header, normally if there is a code in the header it cannot be put in a pre-compiled header, BUT you will still see speed improvement by placing the headers that CAN go into a pch (pre-compiled header).

2) Create a Header file, like this:

>>>>>>>>>>>>>>>>>>>>>>File : "PreCompile.h" >>>>>>>>>>>>>>>>>>>>>>>>>>>

//---------------------------------------------------------------------------

#ifndef PreCompile_H

#define PreCompile_H

//*********PLACE HEADERS THAT CAN GO INTO A PRE-COMPILED HEADER HERE ***********

//#include

//#include

//#include

//#include

//this will add all headers above into the precompiled header every time, this

//list changes it the will slow the compilation down, the PCH needs to be

//re-built

#pragma hdrstop

//PLACE ANY HEADERS HERE THAT WILL NOT GO into the PCH OR #include them outside this header.

//#include

#endif

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

This header file needs to be used in all your source files.

3) Now this header needs to be included in your project through a source file:

>>>>>>>>>>>>>>>>>>>>>>>>>File: "PreCompile.cpp" >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

//set the pre-compiler header to specific file

//this causes an error if -He has not been specified

#pragma checkoption -He

//This specify the name PCH to create

#pragma hdrfile "PreCompile.csm"

//include the the header containing the headers to place in the PCH

#include "PreCompile.H"

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

4) in BCB6 to set the -He switch in the IDE, Project|Edit Option Source and find the CFLAG1 tag, and then add -He option the flags, in BCB2006 Project|Options|C++ Compiler|Pre-compiled headers|Enable precompiled headers with external type files.

5) Then do a build.

6) any subsequent modifications will build faster

The -He compiler switch is most important to use, in that it reduces the amount of symbols that the compiler needs to put into the OBJ files and LIB files.

The big benefit in placing the symbols in a precompiled header (PCH) is that it reduces the number of file accesses, needed to load the symbols, the symbols are read into memory once!�If the symbols were in the OBJ files the linker needs to read the symbols in from EVERY OBJ file!


Author: Roy Nelson