Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 24

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qmake-manual.qdoc -->
  <title>Using Precompiled Headers | qmake Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qmake-manual.html">qmake Manual</a></td><td >Using Precompiled Headers</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qmake-advanced-usage.html" />
  <link rel="next" href="qmake-environment-reference.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qmake-advanced-usage.html">Advanced Usage</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qmake-environment-reference.html">Configuring qmake</a>
</p><p/>
<div class="sidebar">
<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="#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="#mydialog-op-op-ui"><code>mydialog.ui</code></a></li>
<li class="level2"><a href="#stable-op-op-h"><code>stable.h</code></a></li>
<li class="level2"><a href="#myobject-op-op-h"><code>myobject.h</code></a></li>
<li class="level2"><a href="#myobject-op-op-cpp"><code>myobject.cpp</code></a></li>
<li class="level2"><a href="#util-op-op-cpp"><code>util.cpp</code></a></li>
<li class="level2"><a href="#main-op-op-cpp"><code>main.cpp</code></a></li>
<li class="level2"><a href="#precompile-op-op-pro"><code>precompile.pro</code></a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></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 (PCH) 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>qmake supports the use of precompiled headers on some platforms and build environments, including:</p>
<ul>
<li>Windows<ul>
<li>nmake</li>
<li>Visual Studio projects (VS 2008 and later)</li>
</ul>
</li>
<li>macOS, iOS, tvOS, and watchOS<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 id="adding-precompiled-headers-to-your-project">Adding Precompiled Headers to Your Project</h2>
<p>The precompiled header must contain code which is <i>stable</i> and <i>static</i> throughout your project. A typical precompiled header might look like this:</p>
<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><b>Note: </b>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 precompiled headers, you only need to define the <a href="qmake-variable-reference.html#precompiled-header">PRECOMPILED_HEADER</a> variable in your project file:</p>
<pre class="cpp">

  PRECOMPILED_HEADER = stable.h

</pre>
<p>qmake 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 <code>HEADERS</code>, as qmake will do this if the configuration supports precompiled headers.</p>
<p>The MSVC and g++ specs targeting Windows enable <code>precompile_header</code> by default.</p>
<p>Using this option, you may trigger conditional blocks in your project file to add settings when using precompiled headers. For example:</p>
<pre class="cpp">

  precompile_header:!isEmpty(PRECOMPILED_HEADER) {
  DEFINES += USING_PCH
  }

</pre>
<a name="notes-on-possible-issues"></a>
<h2 id="notes-on-possible-issues">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, give distinctive names to header files that will be precompiled.</p>
<a name="example-project"></a><a name="example-project"></a>
<h2 id="example-project">Example Project</h2>
<p>You can find the following source code in the <code>examples/qmake/precompile</code> directory in the Qt distribution:</p>
<a name="mydialog-op-op-ui"></a>
<h3 ><code>mydialog.ui</code></h3>
<p>The following image displays the mydialog.ui file in Qt Creator Design mode. You can view the code in the Edit mode.</p>
<p class="centerAlign"><img src="images/qmake-precompile-ui.png" alt="" /></p><a name="stable-op-op-h"></a>
<h3 ><code>stable.h</code></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="myobject-op-op-h"></a>
<h3 ><code>myobject.h</code></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="../qtcore/qobject.html">QObject</a></span>
  {
  <span class="keyword">public</span>:
      MyObject();
      <span class="operator">~</span>MyObject();
  };

</pre>
<a name="myobject-op-op-cpp"></a>
<h3 ><code>myobject.cpp</code></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="../qtcore/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="util-op-op-cpp"></a>
<h3 ><code>util.cpp</code></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="main-op-op-cpp"></a>
<h3 ><code>main.cpp</code></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="../qtwidgets/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="precompile-op-op-pro"></a>
<h3 ><code>precompile.pro</code></h3>
<pre class="cpp">

  TEMPLATE  <span class="operator">=</span> app
  LANGUAGE  <span class="operator">=</span> C<span class="operator">+</span><span class="operator">+</span>
  CONFIG   <span class="operator">+</span><span class="operator">=</span> console precompile_header
  CONFIG   <span class="operator">-</span><span class="operator">=</span> app_bundle

  <span class="preprocessor"># Use Precompiled headers (PCH)</span>
  PRECOMPILED_HEADER  <span class="operator">=</span> stable<span class="operator">.</span>h

  HEADERS   <span class="operator">=</span> stable<span class="operator">.</span>h \
              mydialog<span class="operator">.</span>h \
              myobject<span class="operator">.</span>h
  SOURCES   <span class="operator">=</span> main<span class="operator">.</span>cpp \
              mydialog<span class="operator">.</span>cpp \
              myobject<span class="operator">.</span>cpp \
              util<span class="operator">.</span>cpp
  FORMS     <span class="operator">=</span> mydialog<span class="operator">.</span>ui

</pre>
</div>
<!-- @@@qmake-precompiledheaders.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qmake-advanced-usage.html">Advanced Usage</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qmake-environment-reference.html">Configuring qmake</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    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.<br>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>