source: trunk/doc/html/qmake-manual-7.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 8.5 KB
RevLine 
[190]1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/qmake/book/qmake-pch.leaf:3 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>Using Precompiled Headers</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><p align="right">[<a href="qmake-manual-6.html">Prev: qmake's Advanced Concepts</a>] [<a href="qmake-manual.html">Home</a>] [<a href="qmake-manual-8.html">Next: qmake Command Reference</a>]</p>
33<h2 align="center">Using Precompiled Headers</h2>
34<a name="About"></a><h3><a name="1"></a>About Precompiled Headers<!-- index About Precompiled Headers --><!-- index Using Precompiled Headers --><!-- index Precompiled Headers --><!-- index PCH --></h3>
35<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>
36<p><em>qmake</em> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
37<ul><li><p>Windows</p>
38<ul><li><p>nmake</p>
39<li><p>Dsp projects (VC 6.0)</p>
40<li><p>Vcproj projects (VC 7.0 &amp; 7.1)</p>
41</ul><li><p>Mac OS X</p>
42<ul><li><p>Makefile</p>
43<li><p>Xcode</p>
44<li><p>GCC 3.3 and up</p>
45</ul><li><p>Unix</p>
46<ul><li><p>GCC 3.4 and up</p>
47</ul></ul><a name="ADD_PCH"></a><h3><a name="2"></a>Adding PCH to your project</h3>
48<a name="PCH_CONTENTS"></a><h4><a name="2-1"></a>Contents of the precompiled header file</h4>
49<p>The precompiled header must contain code which is <em>stable</em> and <em>static</em> throughout your project. A typical PCH might look like this:</p>
50<h5><a name="2-1-1"></a>stable.h</h5>
51<pre>
52 /* Add C includes here */
53
54 #if defined __cplusplus
55 /* Add C++ includes here */
56 #include &lt;stdlib&gt;
57 #include &lt;iostream&gt;
58 #include &lt;vector&gt;
59 #include &lt;qapplication.h&gt; // Qt includes
60 #include &lt;qpushbutton.h&gt;
61 #include &lt;qlabel.h&gt;
62 #include "thirdparty/include/libmain.h"
63 #include "my_stable_class.h"
64 ...
65 #endif
66</pre>
67<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>
68<a name="PROJECT_OPTIONS"></a><h4><a name="2-2"></a>Project options</h4>
69<p>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:</p>
70<pre>
71 PRECOMPILED_HEADER = stable.h
72</pre>
73<p><em>qmake</em> 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>
74<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>
75<pre>
76 precompile_header:!isEmpty(PRECOMPILED_HEADER) {
77 DEFINES += USING_PCH
78 }
79
80</pre>
81<a name="EXAMPLE_PROJECT"></a><h3><a name="3"></a>Example project</h3>
82<p>You can find the following source code in the <em>qt/qmake/examples/precompile</em> directory:</p>
83<p><b>mydialog.ui</b></p>
84<pre> &lt;!DOCTYPE UI&gt;&lt;UI version="3.3" stdsetdef="1"&gt;
85 &lt;class&gt;MyDialog&lt;/class&gt;
86 &lt;widget class="QDialog"&gt;
87 &lt;property name="name"&gt;
88 &lt;cstring&gt;MyDialog&lt;/cstring&gt;
89 &lt;/property&gt;
90 &lt;property name="caption"&gt;
91 &lt;string&gt;Mach 2!&lt;/string&gt;
92 &lt;/property&gt;
93 &lt;vbox&gt;
94 &lt;widget class="QLabel"&gt;
95 &lt;property name="name"&gt;
96 &lt;cstring&gt;aLabel&lt;/cstring&gt;
97 &lt;/property&gt;
98 &lt;property name="text"&gt;
99 &lt;string&gt;Join the life in the fastlane; - PCH enable your project today! -&lt;/string&gt;
100 &lt;/property&gt;
101 &lt;/widget&gt;
102 &lt;widget class="QPushButton"&gt;
103 &lt;property name="name"&gt;
104 &lt;cstring&gt;aButton&lt;/cstring&gt;
105 &lt;/property&gt;
106 &lt;property name="text"&gt;
107 &lt;string&gt;&amp;amp;Quit&lt;/string&gt;
108 &lt;/property&gt;
109 &lt;property name="accel"&gt;
110 &lt;string&gt;Alt+Q&lt;/string&gt;
111 &lt;/property&gt;
112 &lt;/widget&gt;
113 &lt;/vbox&gt;
114 &lt;/widget&gt;
115 &lt;/UI&gt;
116</pre>
117 <p><b>stable.h</b></p>
118<pre> /* Add C includes here */
119
120 #if defined __cplusplus
121 /* Add C++ includes here */
122
123 # include &lt;iostream&gt;
124 # include &lt;qapplication.h&gt;
125 # include &lt;qpushbutton.h&gt;
126 # include &lt;qlabel.h&gt;
127 #endif
128</pre>
129 <p><b>myobject.h</b></p>
130<pre> #include &lt;<a href="qobject-h.html">qobject.h</a>&gt;
131
132 class MyObject : public <a href="qobject.html">QObject</a>
133 {
134 public:
135 MyObject();
136 ~MyObject();
137 };
138</pre>
139 <p><b>myobject.cpp</b></p>
140<pre> #include &lt;iostream&gt;
141 #include &lt;<a href="qobject-h.html">qobject.h</a>&gt;
142 #include "myobject.h"
143
144 MyObject::MyObject()
145 : <a href="qobject.html">QObject</a>()
146 {
147 std::cout &lt;&lt; "MyObject::MyObject()\n";
148 }
149</pre>
150 <p><b>util.cpp</b></p>
151<pre> void util_function_does_nothing()
152 {
153 // Nothing here...
154 int x = 0;
155 ++x;
156 }
157</pre>
158 <p><b>main.cpp</b></p>
159<pre> #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
160 #include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
161 #include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
162 #include "myobject.h"
163 #include "mydialog.h"
164
165 int main(int argc, char **argv)
166 {
167 <a href="qapplication.html">QApplication</a> app(argc, argv);
168
169 MyObject obj;
170 MyDialog dia;
171 app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;dia );
172 dia.connect( dia.aButton, SIGNAL(clicked()), SLOT(<a href="qwidget.html#close">close</a>()) );
173 dia.show();
174
175 return app.<a href="qapplication.html#exec">exec</a>();
176 }
177</pre>
178 <p><b>precompile.pro</b></p>
179<pre> #############################################
180 #
181 # Example for using Precompiled Headers
182 #
183 #############################################
184 TEMPLATE = app
185 LANGUAGE = C++
186 CONFIG += console precompile_header
187
188 # Use Precompiled headers (PCH)
189 PRECOMPILED_HEADER = stable.h
190
191 HEADERS += stable.h \
192 myobject.h
193 SOURCES += main.cpp \
194 myobject.cpp \
195 util.cpp
196 FORMS = mydialog.ui
197</pre>
198<!-- eof -->
199<p align="right">[<a href="qmake-manual-6.html">Prev: qmake's Advanced Concepts</a>] [<a href="qmake-manual.html">Home</a>] [<a href="qmake-manual-8.html">Next: qmake Command Reference</a>]</p>
200<p><address><hr><div align=center>
201<table width=100% cellspacing=0 border=0><tr>
202<td>Copyright &copy; 2007
203<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
204<td align=right><div align=right>Qt 3.3.8</div>
205</table></div></address></body>
206</html>
Note: See TracBrowser for help on using the repository browser.