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 & 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 <stdlib></span>
 <span class="preprocessor">#include <iostream></span>
 <span class="preprocessor">#include <vector></span>
 <span class="preprocessor">#include <QApplication> // Qt includes</span>
 <span class="preprocessor">#include <QPushButton></span>
 <span class="preprocessor">#include <QLabel></span>
 <span class="preprocessor">#include "thirdparty/include/libmain.h"</span>
 <span class="preprocessor">#include "my_stable_class.h"</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"> <ui version="4.0" >
  <author></author>
  <comment></comment>
  <exportmacro></exportmacro>
  <class>MyDialog</class>
  <widget class="QDialog" name="MyDialog" >
   <property name="geometry" >
    <rect>
     <x>0</x>
     <y>0</y>
     <width>401</width>
     <height>70</height>
    </rect>
   </property>
   <property name="windowTitle" >
    <string>Mach 2!</string>
   </property>
   <layout class="QVBoxLayout" >
    <property name="margin" >
     <number>9</number>
    </property>
    <property name="spacing" >
     <number>6</number>
    </property>
    <item>
     <widget class="QLabel" name="aLabel" >
      <property name="text" >
       <string>Join the life in the fastlane; - PCH enable your project today! -</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="aButton" >
      <property name="text" >
       <string>&amp;Quit</string>
      </property>
      <property name="shortcut" >
       <string>Alt+Q</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <pixmapfunction>qPixmapFromMimeSource</pixmapfunction>
  <resources/>
  <connections/>
 </ui></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 <iostream></span>
 <span class="preprocessor"># include <QApplication></span>
 <span class="preprocessor"># include <QPushButton></span>
 <span class="preprocessor"># include <QLabel></span>
 <span class="preprocessor">#endif</span></pre>
<a name=""></a>
<h3><tt>myobject.h</tt></h3>
<pre class="cpp"> <span class="preprocessor">#include <QObject></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 <iostream></span>
 <span class="preprocessor">#include <QDebug></span>
 <span class="preprocessor">#include <QObject></span>
 <span class="preprocessor">#include "myobject.h"</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"><</span><span class="operator"><</span> <span class="string">"MyObject::MyObject()\n"</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 <QApplication></span>
 <span class="preprocessor">#include <QPushButton></span>
 <span class="preprocessor">#include <QLabel></span>
 <span class="preprocessor">#include "myobject.h"</span>
 <span class="preprocessor">#include "mydialog.h"</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">©</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>
 
     |