You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tqt3/doc/html/qmake-manual-7.html

204 lines
8.2 KiB

<!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;ntqapplication.h&gt; // TQt includes
#include &lt;ntqpushbutton.h&gt;
#include &lt;ntqlabel.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="TQDialog"&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="TQLabel"&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="TQPushButton"&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;/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;ntqapplication.h&gt;
# include &lt;ntqpushbutton.h&gt;
# include &lt;ntqlabel.h&gt;
#endif
</pre>
<p><b>myobject.h</b></p>
<pre> #include &lt;<a href="qobject-h.html">ntqobject.h</a>&gt;
class MyObject : public <a href="ntqobject.html">TQObject</a>
{
public:
MyObject();
~MyObject();
};
</pre>
<p><b>myobject.cpp</b></p>
<pre> #include &lt;iostream&gt;
#include &lt;<a href="qobject-h.html">ntqobject.h</a>&gt;
#include "myobject.h"
MyObject::MyObject()
: <a href="ntqobject.html">TQObject</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">ntqapplication.h</a>&gt;
#include &lt;<a href="qpushbutton-h.html">ntqpushbutton.h</a>&gt;
#include &lt;<a href="qlabel-h.html">ntqlabel.h</a>&gt;
#include "myobject.h"
#include "mydialog.h"
int main(int argc, char **argv)
{
<a href="ntqapplication.html">TQApplication</a> app(argc, argv);
MyObject obj;
MyDialog dia;
app.<a href="ntqapplication.html#setMainWidget">setMainWidget</a>( &amp;dia );
dia.connect( dia.aButton, SIGNAL(clicked()), SLOT(<a href="ntqwidget.html#close">close</a>()) );
dia.show();
return app.<a href="ntqapplication.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>TQt 3.3.8</div>
</table></div></address></body>
</html>