1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/qmake-manual.qdoc -->
7
<title>Qt 4.0: Using Precompiled Headers</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Using Precompiled Headers</h1>
21
<a name="about"></a><p>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.</p>
22
<p><tt>qmake</tt> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
26
<li>Dsp projects (VC 6.0)</li>
27
<li>Vcproj projects (VC 7.0 & 7.1)</li>
36
<li>GCC 3.3 and up</li>
40
<a name="add-pch"></a><a name="adding-precompiled-headers-to-your-project"></a>
41
<h2>Adding Precompiled Headers to Your Project</h2>
42
<a name="pch-contents"></a><a name="contents-of-the-precompiled-header-file"></a>
43
<h3>Contents of the Precompiled Header File</h3>
44
<p>The precompiled header must contain code which is <tt>stable</tt> and <tt>static</tt> throughout your project. A typical PCH might look like this:</p>
45
<a name="example"></a>
46
<h4>Example: <tt>stable.h</tt></h4>
47
<pre> // Add C includes here
49
#if defined __cplusplus
50
// Add C++ includes here
51
#include <stdlib>
52
#include <iostream>
53
#include <vector>
54
#include <qapplication.h> // Qt includes
55
#include <qpushbutton.h>
56
#include <qlabel.h>
57
#include "thirdparty/include/libmain.h"
58
#include "my_stable_class.h"
61
<p>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.</p>
62
<a name="project-options"></a><a name="project-options"></a>
63
<h3>Project Options</h3>
64
<p>To make your project use PCH, the only thing you need to change in your project settings (.pro), is to include the <a href="qmake-variable-reference.html#precompiled-header">PRECOMPILED_HEADER</a> option:</p>
65
<pre> PRECOMPILED_HEADER = stable.h</pre>
66
<p><tt>qmake</tt> 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.</p>
67
<p>All platforms that support precompiled headers have the configuration option <b>precompile_header</b> set. Using this option, you may trigger conditional blocks in your .pro file, to add settings when using PCH. For example:</p>
68
<pre> precompile_header:!isEmpty(PRECOMPILED_HEADER) {
71
<a name="example-project"></a><a name="example-project"></a>
72
<h2>Example Project</h2>
73
<p>You can find the following source code in the <tt>qt/qmake/examples/precompile</tt> directory:</p>
75
<h3><tt>mydialog.ui</tt></h3>
76
<pre> <!DOCTYPE UI><UI version="3.3" stdsetdef="1">
77
<class>MyDialog</class>
78
<widget class="QDialog">
79
<property name="name">
80
<cstring>MyDialog</cstring>
82
<property name="caption">
83
<string>Mach 2!</string>
86
<widget class="QLabel">
87
<property name="name">
88
<cstring>aLabel</cstring>
90
<property name="text">
91
<string>Join the life in the fastlane; - PCH enable your project today! -</string>
94
<widget class="QPushButton">
95
<property name="name">
96
<cstring>aButton</cstring>
98
<property name="text">
99
<string>&amp;Quit</string>
101
<property name="accel">
102
<string>Alt+Q</string>
109
<h3><tt>stable.h</tt></h3>
110
<pre> /* Add C includes here */
112
#if defined __cplusplus
113
/* Add C++ includes here */
115
# include <iostream>
116
# include <qapplication.h>
117
# include <qpushbutton.h>
118
# include <qlabel.h>
121
<h3><tt>myobject.h</tt></h3>
122
<pre> #include <qobject.h>
124
class MyObject : public QObject
131
<h3><tt>myobject.cpp</tt></h3>
132
<pre> #include <iostream>
133
#include <qobject.h>
134
#include "myobject.h"
139
std::cout << "MyObject::MyObject()\n";
142
<h3><tt>util.cpp</tt></h3>
143
<pre> void util_function_does_nothing()
150
<h3><tt>main.cpp</tt></h3>
151
<pre> #include <qapplication.h>
152
#include <qpushbutton.h>
153
#include <qlabel.h>
154
#include "myobject.h"
155
#include "mydialog.h"
157
int main(int argc, char **argv)
159
QApplication app(argc, argv);
163
app.setMainWidget(&dia);
164
dia.connect(dia.aButton, SIGNAL(clicked()), SLOT(close()));
170
<h3><tt>precompile.pro</tt></h3>
171
<pre> #############################################
173
# Example for using Precompiled Headers
175
#############################################
178
CONFIG += console precompile_header
180
# Use Precompiled headers (PCH)
181
PRECOMPILED_HEADER = stable.h
183
HEADERS += stable.h \
185
SOURCES += main.cpp \
188
FORMS = mydialog.ui</pre>
189
<p /><address><hr /><div align="center">
190
<table width="100%" cellspacing="0" border="0"><tr class="address">
191
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
192
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
193
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
194
</tr></table></div></address></body>