summaryrefslogtreecommitdiffstats
path: root/doc/html/qmake-manual-7.html
blob: fb5f37dfe8ca3cb8dd4d453acb1ba1d26bd275bb (plain)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/qmake/book/qmake-pch.leaf:3 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Using Precompiled Headers</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>
<h2 align="center">Using Precompiled Headers</h2>
<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>
<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><em>qmake</em> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
<ul><li><p>Windows</p>
<ul><li><p>nmake</p>
<li><p>Dsp projects (VC 6.0)</p>
<li><p>Vcproj projects (VC 7.0 &amp; 7.1)</p>
</ul><li><p>Mac OS X</p>
<ul><li><p>Makefile</p>
<li><p>Xcode</p>
<li><p>GCC 3.3 and up</p>
</ul><li><p>Unix</p>
<ul><li><p>GCC 3.4 and up</p>
</ul></ul><a name="ADD_PCH"></a><h3><a name="2"></a>Adding PCH to your project</h3>
<a name="PCH_CONTENTS"></a><h4><a name="2-1"></a>Contents of the precompiled header file</h4>
<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>
<h5><a name="2-1-1"></a>stable.h</h5>
<pre>
        /* Add C includes here */
        
        #if defined __cplusplus
        /* Add C++ includes here */
        #include &lt;stdlib&gt;
        #include &lt;iostream&gt;
        #include &lt;vector&gt;
        #include &lt;qapplication.h&gt; // Qt includes
        #include &lt;qpushbutton.h&gt;
        #include &lt;qlabel.h&gt;
        #include "thirdparty/include/libmain.h"
        #include "my_stable_class.h"
        ...
        #endif
</pre>
<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>
<a name="PROJECT_OPTIONS"></a><h4><a name="2-2"></a>Project options</h4>
<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>
<pre>
        PRECOMPILED_HEADER = stable.h
</pre>
<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>
<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>
<pre>
        precompile_header:!isEmpty(PRECOMPILED_HEADER) {
            DEFINES += USING_PCH
        }

</pre>
<a name="EXAMPLE_PROJECT"></a><h3><a name="3"></a>Example project</h3>
<p>You can find the following source code in the <em>qt/qmake/examples/precompile</em> directory:</p>
<p><b>mydialog.ui</b></p>
<pre>    &lt;!DOCTYPE UI&gt;&lt;UI version="3.3" stdsetdef="1"&gt;
    &lt;class&gt;MyDialog&lt;/class&gt;
    &lt;widget class="QDialog"&gt;
        &lt;property name="name"&gt;
            &lt;cstring&gt;MyDialog&lt;/cstring&gt;
        &lt;/property&gt;
        &lt;property name="caption"&gt;
            &lt;string&gt;Mach 2!&lt;/string&gt;
        &lt;/property&gt;
        &lt;vbox&gt;
            &lt;widget class="QLabel"&gt;
                &lt;property name="name"&gt;
                    &lt;cstring&gt;aLabel&lt;/cstring&gt;
                &lt;/property&gt;
                &lt;property name="text"&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;widget class="QPushButton"&gt;
                &lt;property name="name"&gt;
                    &lt;cstring&gt;aButton&lt;/cstring&gt;
                &lt;/property&gt;
                &lt;property name="text"&gt;
                    &lt;string&gt;&amp;amp;Quit&lt;/string&gt;
                &lt;/property&gt;
                &lt;property name="accel"&gt;
                    &lt;string&gt;Alt+Q&lt;/string&gt;
                &lt;/property&gt;
            &lt;/widget&gt;
        &lt;/vbox&gt;
    &lt;/widget&gt;
    &lt;/UI&gt;
</pre>
 <p><b>stable.h</b></p>
<pre>    /* Add C includes here */

    #if defined __cplusplus
    /* Add C++ includes here */

    # include &lt;iostream&gt;
    # include &lt;qapplication.h&gt;
    # include &lt;qpushbutton.h&gt;
    # include &lt;qlabel.h&gt;
    #endif
</pre>
 <p><b>myobject.h</b></p>
<pre>    #include &lt;<a href="qobject-h.html">qobject.h</a>&gt;

    class MyObject : public <a href="qobject.html">QObject</a>
    {
    public:
        MyObject();
        ~MyObject();
    };
</pre>
 <p><b>myobject.cpp</b></p>
<pre>    #include &lt;iostream&gt;
    #include &lt;<a href="qobject-h.html">qobject.h</a>&gt;
    #include "myobject.h"

    MyObject::MyObject()
        : <a href="qobject.html">QObject</a>()
    {
        std::cout &lt;&lt; "MyObject::MyObject()\n";
    }
</pre>
 <p><b>util.cpp</b></p>
<pre>    void util_function_does_nothing()
    {
        // Nothing here...
        int x = 0;
        ++x;
    }
</pre>
 <p><b>main.cpp</b></p>
<pre>    #include &lt;<a href="qapplication-h.html">qapplication.h</a>&gt;
    #include &lt;<a href="qpushbutton-h.html">qpushbutton.h</a>&gt;
    #include &lt;<a href="qlabel-h.html">qlabel.h</a>&gt;
    #include "myobject.h"
    #include "mydialog.h"

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

        MyObject obj;
        MyDialog dia;
        app.<a href="qapplication.html#setMainWidget">setMainWidget</a>( &amp;dia );
        dia.connect( dia.aButton, SIGNAL(clicked()), SLOT(<a href="qwidget.html#close">close</a>()) );
        dia.show();

        return app.<a href="qapplication.html#exec">exec</a>();
    }
</pre>
 <p><b>precompile.pro</b></p>
<pre>    #############################################
    #
    # Example for using Precompiled Headers
    #
    #############################################
    TEMPLATE  = app
    LANGUAGE  = C++
    CONFIG   += console precompile_header

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

    HEADERS  += stable.h \
                myobject.h
    SOURCES  += main.cpp \
                myobject.cpp \
                util.cpp
    FORMS     = mydialog.ui
</pre>
<!-- eof -->
<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>
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>