\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
    \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. When compiling Qt programs, it
may be wise to add an include to \e qt.h in your precompiled header,
to significantly increase compilation speed. 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 <qt.h> // Includes all of Qt
	#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 C files and CPP files cannot share the same
precompiled header file.


\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 PRECOMPH option:
\code
	PRECOMPH = stable.h
\endcode
\e qmake will handle the rest, to ensure the creation and use of the
precompiled header file. 


\target ADVANCED_WINDOWS_OPTION
\section3 Advanced Windows option

\e{This is used on Windows only}

On the Windows platform, it is common to generate the precompiled
header file, based on a specified \c{.cpp} file. This is because MSVC
also lets you precompile functions and whole classes, to further
increase compilation speed.

You can  specify your own \c{.cpp} file like this:
\code
	PRECOMPCPP = stable.cpp
\endcode

Note that with qmake, you don't have to specify this \c{.cpp} file,
since \e qmake will generate one for you, which will include the
header file specified in \c PRECOMPH. Do not use this feature for
multiplatform projects.

\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
