\chapter Using Precompiled Headers

\target About
\section1 About Precompiled Headers
\index About Precompiled Headers
\index Using Precompiled Headers
\index Precompiled Headers
\index PCH

Precompiled headers 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. Each subsequent compilation is faster because the
stable code does not need to be recompiled.

\e qmake supports the use of precompiled headers (PCH) on some
platforms and build environments, including:
\list
\i Windows
    \list
    \i nmake
    \i Dsp projects (VC 6.0)
    \i Vcproj projects (VC 7.0 \& 7.1)
    \endlist
\i Mac OS X
    \list
    \i Makefile
    \i Xcode
    \i GCC 3.3 and up
    \endlist
\i Unix
    \list
    \i GCC 3.4 and up
    \endlist
\endlist


\target ADD_PCH
\section1 Adding PCH to your project


\target PCH_CONTENTS
\section2 Contents of the precompiled header file

The precompiled header must contain code which is \e stable
and \e static throughout your project. A typical PCH might look
like this:
\section3 stable.h

\code
	/* Add C includes here */
	
	#if defined __cplusplus
	/* Add C++ includes here */
	#include <stdlib>
	#include <iostream>
	#include <vector>
	#include <ntqapplication.h> // TQt includes
	#include <ntqpushbutton.h>
	#include <ntqlabel.h>
	#include "thirdparty/include/libmain.h"
	#include "my_stable_class.h"
	...
	#endif
\endcode

Note that a precompiled header file needs to separate C includes from
CPP includes, since the precompiled header file for C files may not
contain C++ code.


\target PROJECT_OPTIONS
\section2 Project options

To make your project use PCH, the only thing you need to change in
your project settings (.pro), is to include the PRECOMPILED_HEADER option:
\code
	PRECOMPILED_HEADER = stable.h
\endcode
\e qmake will handle the rest, to ensure the creation and use of the
precompiled header file. You do not need to include the precompiled
header file in HEADERS, as qmake will do this if the configuration
supports PCH.

All platforms that support precompiled headers have the configuration
option \Bold precompile_header set. Using this option, you may trigger
conditional blocks in your .pro file, to add settings when using PCH.
For example:
\code
	precompile_header:!isEmpty(PRECOMPILED_HEADER) {
	    DEFINES += USING_PCH
	}

\endcode

\target EXAMPLE_PROJECT
\section1 Example project

You can find the following source code in the
\e{qt/qmake/examples/precompile} directory:

\Bold mydialog.ui
\quotefile precompile/mydialog.ui
\skipto <!
\printuntil </UI>

\Bold stable.h
\quotefile precompile/stable.h
\skipto /*
\printuntil #endif

\Bold myobject.h
\quotefile precompile/myobject.h
\skipto #include
\printuntil }

\Bold myobject.cpp
\quotefile precompile/myobject.cpp
\skipto #include
\printuntil }

\Bold util.cpp
\quotefile precompile/util.cpp
\skipto void
\printuntil }

\Bold main.cpp
\quotefile precompile/main.cpp
\skipto #include
\printuntil }

\Bold precompile.pro
\quotefile precompile/precompile.pro
\skipto #
\printuntil .ui
