File: qmake-precompiledheaders.html

package info (click to toggle)
qt4-x11 4%3A4.8.2%2Bdfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 701,696 kB
  • sloc: cpp: 2,686,179; ansic: 375,485; python: 25,859; sh: 19,349; xml: 17,091; perl: 14,765; yacc: 5,383; asm: 5,038; makefile: 1,259; lex: 555; ruby: 526; objc: 347; cs: 112; pascal: 112; php: 54; sed: 34
file content (270 lines) | stat: -rw-r--r-- 12,763 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qmake-manual.qdoc -->
  <title>Qt 4.8: Using Precompiled Headers</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="content"> 
    <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
  </div>
  <div class="breadcrumb toolblock">
    <ul>
      <li class="first"><a href="index.html">Home</a></li>
      <!--  Breadcrumbs go here -->
<li>Using Precompiled Headers</li>
    </ul>
  </div>
</div>
<div class="content mainContent">
  <link rel="prev" href="qmake-advanced-usage.html" />
  <link rel="next" href="qmake-reference.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qmake-advanced-usage.html">qmake Advanced Usage</a>
<a class="nextPage" href="qmake-reference.html">qmake Reference</a>
</p><p/>
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#adding-precompiled-headers-to-your-project">Adding Precompiled Headers to Your Project</a></li>
<li class="level2"><a href="#contents-of-the-precompiled-header-file">Contents of the Precompiled Header File</a></li>
<li class="level3"><a href="#example">Example: <tt>stable.h</tt></a></li>
<li class="level2"><a href="#project-options">Project Options</a></li>
<li class="level1"><a href="#notes-on-possible-issues">Notes on Possible Issues</a></li>
<li class="level1"><a href="#example-project">Example Project</a></li>
<li class="level2"><a href="#"><tt>mydialog.ui</tt></a></li>
<li class="level2"><a href="#"><tt>stable.h</tt></a></li>
<li class="level2"><a href="#"><tt>myobject.h</tt></a></li>
<li class="level2"><a href="#"><tt>myobject.cpp</tt></a></li>
<li class="level2"><a href="#"><tt>util.cpp</tt></a></li>
<li class="level2"><a href="#"><tt>main.cpp</tt></a></li>
<li class="level2"><a href="#"><tt>precompile.pro</tt></a></li>
</ul>
</div>
<h1 class="title">Using Precompiled Headers</h1>
<span class="subtitle"></span>
<!-- $$$qmake-precompiledheaders.html-description -->
<div class="descr"> <a name="details"></a>
<a name="introduction"></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>
<p><a href="qmake-manual.html#qmake"><tt>qmake</tt></a> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
<ul>
<li>Windows<ul>
<li>nmake</li>
<li>Dsp projects (VC 6.0)</li>
<li>Vcproj projects (VC 7.0 &amp; 7.1)</li>
</ul>
</li>
<li>Mac OS X<ul>
<li>Makefile</li>
<li>Xcode</li>
</ul>
</li>
<li>Unix<ul>
<li>GCC 3.4 and above</li>
</ul>
</li>
</ul>
<a name="add-pch"></a><a name="adding-precompiled-headers-to-your-project"></a>
<h2>Adding Precompiled Headers to Your Project</h2>
<a name="pch-contents"></a><a name="contents-of-the-precompiled-header-file"></a>
<h3>Contents of the Precompiled Header File</h3>
<p>The precompiled header must contain code which is <i>stable</i> and <i>static</i> throughout your project. A typical PCH might look like this:</p>
<a name="example"></a>
<h4>Example: <tt>stable.h</tt></h4>
<pre class="cpp"> <span class="comment">// Add C includes here</span>

 <span class="preprocessor">#if defined __cplusplus</span>
 <span class="comment">// Add C++ includes here</span>
 <span class="preprocessor">#include &lt;stdlib&gt;</span>
 <span class="preprocessor">#include &lt;iostream&gt;</span>
 <span class="preprocessor">#include &lt;vector&gt;</span>
 <span class="preprocessor">#include &lt;QApplication&gt; // Qt includes</span>
 <span class="preprocessor">#include &lt;QPushButton&gt;</span>
 <span class="preprocessor">#include &lt;QLabel&gt;</span>
 <span class="preprocessor">#include &quot;thirdparty/include/libmain.h&quot;</span>
 <span class="preprocessor">#include &quot;my_stable_class.h&quot;</span>
 <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
 <span class="preprocessor">#endif</span></pre>
<p>Note that a precompiled header file needs to separate C includes from C++ includes, since the precompiled header file for C files may not contain C++ code.</p>
<a name="project-options"></a><a name="project-options"></a>
<h3>Project Options</h3>
<p>To make your project use PCH, you only need to define the <tt>PRECOMPILED_HEADER</tt> variable in your project file:</p>
<pre class="cpp"> PRECOMPILED_HEADER = stable.h</pre>
<p><a href="qmake-manual.html#qmake"><tt>qmake</tt></a> 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 <tt>HEADERS</tt>, as <a href="qmake-manual.html#qmake"><tt>qmake</tt></a> will do this if the configuration supports PCH.</p>
<p>All platforms that support precompiled headers have the configuration option <tt>precompile_header</tt> set. Using this option, you may trigger conditional blocks in your project file to add settings when using PCH. For example:</p>
<pre class="cpp"> precompile_header:!isEmpty(PRECOMPILED_HEADER) {
 DEFINES += USING_PCH
 }</pre>
<a name="notes-on-possible-issues"></a>
<h2>Notes on Possible Issues</h2>
<p>On some platforms, the file name suffix for precompiled header files is the same as that for other object files. For example, the following declarations may cause two different object files with the same name to be generated:</p>
<pre class="cpp"> PRECOMPILED_HEADER = window.h
 SOURCES            = window.cpp</pre>
<p>To avoid potential conflicts like these, it is good practice to ensure that header files that will be precompiled are given distinctive names.</p>
<a name="example-project"></a><a name="example-project"></a>
<h2>Example Project</h2>
<p>You can find the following source code in the <tt>examples/qmake/precompile</tt> directory in the Qt distribution:</p>
<a name=""></a>
<h3><tt>mydialog.ui</tt></h3>
<pre class="qml"> &lt;ui version=&quot;4.0&quot; &gt;
  &lt;author&gt;&lt;/author&gt;
  &lt;comment&gt;&lt;/comment&gt;
  &lt;exportmacro&gt;&lt;/exportmacro&gt;
  &lt;class&gt;MyDialog&lt;/class&gt;
  &lt;widget class=&quot;QDialog&quot; name=&quot;MyDialog&quot; &gt;
   &lt;property name=&quot;geometry&quot; &gt;
    &lt;rect&gt;
     &lt;x&gt;0&lt;/x&gt;
     &lt;y&gt;0&lt;/y&gt;
     &lt;width&gt;401&lt;/width&gt;
     &lt;height&gt;70&lt;/height&gt;
    &lt;/rect&gt;
   &lt;/property&gt;
   &lt;property name=&quot;windowTitle&quot; &gt;
    &lt;string&gt;Mach 2!&lt;/string&gt;
   &lt;/property&gt;
   &lt;layout class=&quot;QVBoxLayout&quot; &gt;
    &lt;property name=&quot;margin&quot; &gt;
     &lt;number&gt;9&lt;/number&gt;
    &lt;/property&gt;
    &lt;property name=&quot;spacing&quot; &gt;
     &lt;number&gt;6&lt;/number&gt;
    &lt;/property&gt;
    &lt;item&gt;
     &lt;widget class=&quot;QLabel&quot; name=&quot;aLabel&quot; &gt;
      &lt;property name=&quot;text&quot; &gt;
       &lt;string&gt;Join the life in the fastlane; - PCH enable your project today! -&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/item&gt;
    &lt;item&gt;
     &lt;widget class=&quot;QPushButton&quot; name=&quot;aButton&quot; &gt;
      &lt;property name=&quot;text&quot; &gt;
       &lt;string&gt;&amp;amp;Quit&lt;/string&gt;
      &lt;/property&gt;
      &lt;property name=&quot;shortcut&quot; &gt;
       &lt;string&gt;Alt+Q&lt;/string&gt;
      &lt;/property&gt;
     &lt;/widget&gt;
    &lt;/item&gt;
   &lt;/layout&gt;
  &lt;/widget&gt;
  &lt;pixmapfunction&gt;qPixmapFromMimeSource&lt;/pixmapfunction&gt;
  &lt;resources/&gt;
  &lt;connections/&gt;
 &lt;/ui&gt;</pre>
<a name=""></a>
<h3><tt>stable.h</tt></h3>
<pre class="cpp"> <span class="comment">/* Add C includes here */</span>

 <span class="preprocessor">#if defined __cplusplus</span>
 <span class="comment">/* Add C++ includes here */</span>

 <span class="preprocessor"># include &lt;iostream&gt;</span>
 <span class="preprocessor"># include &lt;QApplication&gt;</span>
 <span class="preprocessor"># include &lt;QPushButton&gt;</span>
 <span class="preprocessor"># include &lt;QLabel&gt;</span>
 <span class="preprocessor">#endif</span></pre>
<a name=""></a>
<h3><tt>myobject.h</tt></h3>
<pre class="cpp"> <span class="preprocessor">#include &lt;QObject&gt;</span>

 <span class="keyword">class</span> MyObject : <span class="keyword">public</span> <span class="type"><a href="qobject.html">QObject</a></span>
 {
 <span class="keyword">public</span>:
     MyObject();
     <span class="operator">~</span>MyObject();
 };</pre>
<a name=""></a>
<h3><tt>myobject.cpp</tt></h3>
<pre class="cpp"> <span class="preprocessor">#include &lt;iostream&gt;</span>
 <span class="preprocessor">#include &lt;QDebug&gt;</span>
 <span class="preprocessor">#include &lt;QObject&gt;</span>
 <span class="preprocessor">#include &quot;myobject.h&quot;</span>

 MyObject<span class="operator">::</span>MyObject()
     : <span class="type"><a href="qobject.html">QObject</a></span>()
 {
     std<span class="operator">::</span>cout <span class="operator">&lt;</span><span class="operator">&lt;</span> <span class="string">&quot;MyObject::MyObject()\n&quot;</span>;
 }</pre>
<a name=""></a>
<h3><tt>util.cpp</tt></h3>
<pre class="cpp"> <span class="type">void</span> util_function_does_nothing()
 {
     <span class="comment">// Nothing here...</span>
     <span class="type">int</span> x <span class="operator">=</span> <span class="number">0</span>;
     <span class="operator">+</span><span class="operator">+</span>x;
 }</pre>
<a name=""></a>
<h3><tt>main.cpp</tt></h3>
<pre class="cpp"> <span class="preprocessor">#include &lt;QApplication&gt;</span>
 <span class="preprocessor">#include &lt;QPushButton&gt;</span>
 <span class="preprocessor">#include &lt;QLabel&gt;</span>
 <span class="preprocessor">#include &quot;myobject.h&quot;</span>
 <span class="preprocessor">#include &quot;mydialog.h&quot;</span>

 <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span><span class="operator">*</span>argv)
 {
     <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);

     MyObject obj;
     MyDialog dialog;

     dialog<span class="operator">.</span>connect(dialog<span class="operator">.</span>aButton<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> SLOT(close()));
     dialog<span class="operator">.</span>show();

     <span class="keyword">return</span> app<span class="operator">.</span>exec();
 }</pre>
<a name=""></a>
<h3><tt>precompile.pro</tt></h3>
<pre class="cpp"> TEMPLATE  = app
 LANGUAGE  = C++
 CONFIG   += console precompile_header

 # Use Precompiled headers (PCH)
 PRECOMPILED_HEADER  = stable.h

 HEADERS   = stable.h \
             mydialog.h \
             myobject.h
 SOURCES   = main.cpp \
             mydialog.cpp \
             myobject.cpp \
             util.cpp
 FORMS     = mydialog.ui</pre>
</div>
<!-- @@@qmake-precompiledheaders.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qmake-advanced-usage.html">qmake Advanced Usage</a>
<a class="nextPage" href="qmake-reference.html">qmake Reference</a>
</p>
  <div class="ft">
    <span></span>
  </div>
</div> 
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2012 Nokia Corporation and/or its
      subsidiaries. Documentation contributions included herein are the copyrights of
      their respective owners.</p>
    <br />
    <p>
      The documentation provided herein is licensed under the terms of the
      <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
      License version 1.3</a> as published by the Free Software Foundation.</p>
    <p>
      Documentation sources may be obtained from <a href="http://www.qt-project.org">
      www.qt-project.org</a>.</p>
    <br />
    <p>
      Nokia, Qt and their respective logos are trademarks of Nokia Corporation 
      in Finland and/or other countries worldwide. All other trademarks are property
      of their respective owners. <a title="Privacy Policy"
      href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>