~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qmake-precompiledheaders.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/qmake-manual.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: Using Precompiled Headers</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">Using Precompiled Headers</h1>
 
21
<a name="about"></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>
 
22
<p><tt>qmake</tt> supports the use of precompiled headers (PCH) on some platforms and build environments, including:</p>
 
23
<ul>
 
24
<li>Windows<ul>
 
25
<li>nmake</li>
 
26
<li>Dsp projects (VC 6.0)</li>
 
27
<li>Vcproj projects (VC 7.0 &amp; 7.1)</li>
 
28
</ul>
 
29
</li>
 
30
<li>Mac OS X<ul>
 
31
<li>Makefile</li>
 
32
<li>Xcode</li>
 
33
</ul>
 
34
</li>
 
35
<li>Unix<ul>
 
36
<li>GCC 3.3 and up</li>
 
37
</ul>
 
38
</li>
 
39
</ul>
 
40
<a name="add-pch"></a><a name="adding-precompiled-headers-to-your-project"></a>
 
41
<h2>Adding Precompiled Headers to Your Project</h2>
 
42
<a name="pch-contents"></a><a name="contents-of-the-precompiled-header-file"></a>
 
43
<h3>Contents of the Precompiled Header File</h3>
 
44
<p>The precompiled header must contain code which is <tt>stable</tt> and <tt>static</tt> throughout your project. A typical PCH might look like this:</p>
 
45
<a name="example"></a>
 
46
<h4>Example: <tt>stable.h</tt></h4>
 
47
<pre>&nbsp;   // Add C includes here
 
48
 
 
49
    #if defined __cplusplus
 
50
    // Add C++ includes here
 
51
    #include &lt;stdlib&gt;
 
52
    #include &lt;iostream&gt;
 
53
    #include &lt;vector&gt;
 
54
    #include &lt;qapplication.h&gt; // Qt includes
 
55
    #include &lt;qpushbutton.h&gt;
 
56
    #include &lt;qlabel.h&gt;
 
57
    #include &quot;thirdparty/include/libmain.h&quot;
 
58
    #include &quot;my_stable_class.h&quot;
 
59
    ...
 
60
    #endif</pre>
 
61
<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>
 
62
<a name="project-options"></a><a name="project-options"></a>
 
63
<h3>Project Options</h3>
 
64
<p>To make your project use PCH, the only thing you need to change in your project settings (.pro), is to include the <a href="qmake-variable-reference.html#precompiled-header">PRECOMPILED_HEADER</a> option:</p>
 
65
<pre>&nbsp;       PRECOMPILED_HEADER = stable.h</pre>
 
66
<p><tt>qmake</tt> 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>
 
67
<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>
 
68
<pre>&nbsp;       precompile_header:!isEmpty(PRECOMPILED_HEADER) {
 
69
        DEFINES += USING_PCH
 
70
        }</pre>
 
71
<a name="example-project"></a><a name="example-project"></a>
 
72
<h2>Example Project</h2>
 
73
<p>You can find the following source code in the <tt>qt/qmake/examples/precompile</tt> directory:</p>
 
74
<a name=""></a>
 
75
<h3><tt>mydialog.ui</tt></h3>
 
76
<pre>&nbsp;   &lt;!DOCTYPE UI&gt;&lt;UI version=&quot;3.3&quot; stdsetdef=&quot;1&quot;&gt;
 
77
    &lt;class&gt;MyDialog&lt;/class&gt;
 
78
    &lt;widget class=&quot;QDialog&quot;&gt;
 
79
        &lt;property name=&quot;name&quot;&gt;
 
80
            &lt;cstring&gt;MyDialog&lt;/cstring&gt;
 
81
        &lt;/property&gt;
 
82
        &lt;property name=&quot;caption&quot;&gt;
 
83
            &lt;string&gt;Mach 2!&lt;/string&gt;
 
84
        &lt;/property&gt;
 
85
        &lt;vbox&gt;
 
86
            &lt;widget class=&quot;QLabel&quot;&gt;
 
87
                &lt;property name=&quot;name&quot;&gt;
 
88
                    &lt;cstring&gt;aLabel&lt;/cstring&gt;
 
89
                &lt;/property&gt;
 
90
                &lt;property name=&quot;text&quot;&gt;
 
91
                    &lt;string&gt;Join the life in the fastlane; - PCH enable your project today! -&lt;/string&gt;
 
92
                &lt;/property&gt;
 
93
            &lt;/widget&gt;
 
94
            &lt;widget class=&quot;QPushButton&quot;&gt;
 
95
                &lt;property name=&quot;name&quot;&gt;
 
96
                    &lt;cstring&gt;aButton&lt;/cstring&gt;
 
97
                &lt;/property&gt;
 
98
                &lt;property name=&quot;text&quot;&gt;
 
99
                    &lt;string&gt;&amp;amp;Quit&lt;/string&gt;
 
100
                &lt;/property&gt;
 
101
                &lt;property name=&quot;accel&quot;&gt;
 
102
                    &lt;string&gt;Alt+Q&lt;/string&gt;
 
103
                &lt;/property&gt;
 
104
            &lt;/widget&gt;
 
105
        &lt;/vbox&gt;
 
106
    &lt;/widget&gt;
 
107
    &lt;/UI&gt;</pre>
 
108
<a name=""></a>
 
109
<h3><tt>stable.h</tt></h3>
 
110
<pre>&nbsp;   /* Add C includes here */
 
111
 
 
112
    #if defined __cplusplus
 
113
    /* Add C++ includes here */
 
114
 
 
115
    # include &lt;iostream&gt;
 
116
    # include &lt;qapplication.h&gt;
 
117
    # include &lt;qpushbutton.h&gt;
 
118
    # include &lt;qlabel.h&gt;
 
119
    #endif</pre>
 
120
<a name=""></a>
 
121
<h3><tt>myobject.h</tt></h3>
 
122
<pre>&nbsp;   #include &lt;qobject.h&gt;
 
123
 
 
124
    class MyObject : public QObject
 
125
    {
 
126
    public:
 
127
        MyObject();
 
128
        ~MyObject();
 
129
    };</pre>
 
130
<a name=""></a>
 
131
<h3><tt>myobject.cpp</tt></h3>
 
132
<pre>&nbsp;   #include &lt;iostream&gt;
 
133
    #include &lt;qobject.h&gt;
 
134
    #include &quot;myobject.h&quot;
 
135
 
 
136
    MyObject::MyObject()
 
137
        : QObject()
 
138
    {
 
139
        std::cout &lt;&lt; &quot;MyObject::MyObject()\n&quot;;
 
140
    }</pre>
 
141
<a name=""></a>
 
142
<h3><tt>util.cpp</tt></h3>
 
143
<pre>&nbsp;   void util_function_does_nothing()
 
144
    {
 
145
        // Nothing here...
 
146
        int x = 0;
 
147
        ++x;
 
148
    }</pre>
 
149
<a name=""></a>
 
150
<h3><tt>main.cpp</tt></h3>
 
151
<pre>&nbsp;   #include &lt;qapplication.h&gt;
 
152
    #include &lt;qpushbutton.h&gt;
 
153
    #include &lt;qlabel.h&gt;
 
154
    #include &quot;myobject.h&quot;
 
155
    #include &quot;mydialog.h&quot;
 
156
 
 
157
    int main(int argc, char **argv)
 
158
    {
 
159
        QApplication app(argc, argv);
 
160
 
 
161
        MyObject obj;
 
162
        MyDialog dia;
 
163
        app.setMainWidget(&amp;dia);
 
164
        dia.connect(dia.aButton, SIGNAL(clicked()), SLOT(close()));
 
165
        dia.show();
 
166
 
 
167
        return app.exec();
 
168
    }</pre>
 
169
<a name=""></a>
 
170
<h3><tt>precompile.pro</tt></h3>
 
171
<pre>&nbsp;   #############################################
 
172
    #
 
173
    # Example for using Precompiled Headers
 
174
    #
 
175
    #############################################
 
176
    TEMPLATE  = app
 
177
    LANGUAGE  = C++
 
178
    CONFIG   += console precompile_header
 
179
 
 
180
    # Use Precompiled headers (PCH)
 
181
    PRECOMPILED_HEADER  = stable.h
 
182
 
 
183
    HEADERS  += stable.h \
 
184
                myobject.h
 
185
    SOURCES  += main.cpp \
 
186
                myobject.cpp \
 
187
                util.cpp
 
188
    FORMS     = mydialog.ui</pre>
 
189
<p /><address><hr /><div align="center">
 
190
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
191
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
192
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
193
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
194
</tr></table></div></address></body>
 
195
</html>