source: trunk/doc/html/qmake-manual-4.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 9.3 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/qmake/book/qmake-tutorial.leaf:3 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>qmake Tutorial</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<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-3.html">Prev: The 10 minute guide to using qmake</a>] [<a href="qmake-manual.html">Home</a>] [<a href="qmake-manual-5.html">Next: qmake Concepts</a>]</p>
33<h2 align="center">qmake Tutorial</h2>
34<h3><a name="1"></a>Introduction to the qmake tutorial</h3>
35<p>This tutorial teaches you how to use <em>qmake</em>. We recommend that you read the <em>qmake</em> user guide after completing this tutorial.</p>
36<h3><a name="2"></a>Starting off simple</h3>
37<p>Let's assume that you have just finished a basic implementation of your application, and you have created the following files:</p>
38<ul><li><p>hello.cpp</p>
39<li><p>hello.h</p>
40<li><p>main.cpp</p>
41</ul><p>You will find these files in <em>qt/qmake/examples/tutorial</em>. The only other thing you know about the setup of the application is that it's written in Qt. First, using your favorite plain text editor, create a file called <em>hello.pro</em> in <em>qt/qmake/tutorial</em>. The first thing you need to do is add the lines that tell <em>qmake</em> about the source and header files that are part of your development project.</p>
42<p>We'll add the source files to the project file first. To do this you need to use the SOURCES variable. Just start a new line with <em>SOURCES +=</em> and put hello.cpp after it. You should have something like:</p>
43<pre>
44 SOURCES += hello.cpp
45</pre>
46<p>We repeat this for each source file in the project, until we end up with:</p>
47<pre>
48 SOURCES += hello.cpp
49 SOURCES += main.cpp
50</pre>
51<p>If you prefer to use a Make-like syntax, with all the files listed in one go you can use the newline escaping like this:</p>
52<pre>
53 SOURCES = hello.cpp \
54 main.cpp
55</pre>
56<p>Now that the source files are listed in the project file, the header files must be added. These are added in exactly the same way as source files, except that the variable name is HEADERS:</p>
57<p>Once you have done this, your project file should look something like this:</p>
58<pre>
59 HEADERS += hello.h
60 SOURCES += hello.cpp
61 SOURCES += main.cpp
62</pre>
63<p>The target name is set automatically; it is the same as the project file, but with the suffix appropriate to the platform. For example, if the project file is called 'hello.pro', the target will be 'hello.exe' on Windows and 'hello' on Unix. If you want to use a different name you can set it in the project file:</p>
64<pre>
65 TARGET = helloworld
66</pre>
67<p>The final step is to set the <em>CONFIG</em> variable. Since this is a Qt application, we need to put 'qt' on the CONFIG line so that <em>qmake</em> will add the relevant libraries to be linked against and ensure that build lines for <em>moc</em> and <em>uic</em> are included in the makefile.</p>
68<p>The finished project file should look like this:</p>
69<pre>
70 CONFIG += qt
71 HEADERS += hello.h
72 SOURCES += hello.cpp
73 SOURCES += main.cpp
74</pre>
75<p>You can now use <em>qmake</em> to generate a makefile for your application. On the command line, in your application directory, type:</p>
76<pre>
77 qmake -o Makefile hello.pro
78</pre>
79<p>Then type <em>make</em> or <em>nmake</em> depending on the compiler you use.</p>
80<h3><a name="3"></a>Making an application debuggable</h3>
81<p>The release version of an application doesn't contain any debugging symbols or other debuggin information. During development it is useful to produce a debugging version of the application that has the relevant information. This is easily achieved by adding 'debug' to the CONFIG variable in the project file.</p>
82<p>For example:</p>
83<pre>
84 CONFIG += qt debug
85 HEADERS += hello.h
86 SOURCES += hello.cpp
87 SOURCES += main.cpp
88</pre>
89<p>Use <em>qmake</em> as before to generate a makefile and you will be able to debug your application.</p>
90<h3><a name="4"></a>Adding platform specific source files</h3>
91<p>After a few hours of coding, you might have made a start on the platform specific part of your application, and decided to keep the platform dependent code separate. So you now have two new files to include into your project file - <em>hellowin.cpp</em> and <em>hellounix.cpp</em>. We can't just add these to the <em>SOURCES</em> variable since this will put both files in the makefile. So what we need to do here is to use a scope which will be processed depending on which platform <em>qmake</em> is run on.</p>
92<p>A simple scope which will add in the platform dependent file for Windows looks like this:</p>
93<pre>
94 win32 {
95 SOURCES += hellowin.cpp
96 }
97</pre>
98<p>So if <em>qmake</em> is run on Windows, it will add <em>hellowin.cpp</em> to the list of source files. If <em>qmake</em> is run on any other platform, it will simply ignore it. Now all that is left to be done is to create a scope for the unix dependent file.</p>
99<p>When you have done that, your project file should now look something like this:</p>
100<pre>
101 CONFIG += qt debug
102 HEADERS += hello.h
103 SOURCES += hello.cpp
104 SOURCES += main.cpp
105 win32 {
106 SOURCES += hellowin.cpp
107 }
108 unix {
109 SOURCES += hellounix.cpp
110 }
111</pre>
112<p>Use <em>qmake</em> as before to generate a makefile.</p>
113<h3><a name="5"></a>Stopping qmake if a file doesn't exist</h3>
114<p>You may not want to create a makefile if a certain file doesn't exist. We can check if a file exists by using the exists() function. We can stop <em>qmake</em> from processing by using the error() function. This works in the same way as scopes. Simply replace the scope condition with the function. A check for a main.cpp file looks like this:</p>
115<pre>
116 !exists( main.cpp ) {
117 error( "No main.cpp file found" )
118 }
119</pre>
120<p>The "!" is used to negate the test, i.e. <tt>exists( main.cpp )</tt> is true if the file exists and <tt>!exists( main.cpp )</tt> is true if the file doesn't exist.</p>
121<pre>
122 CONFIG += qt debug
123 HEADERS += hello.h
124 SOURCES += hello.cpp
125 SOURCES += main.cpp
126 win32 {
127 SOURCES += hellowin.cpp
128 }
129 unix {
130 SOURCES += hellounix.cpp
131 }
132 !exists( main.cpp ) {
133 error( "No main.cpp file found" )
134 }
135</pre>
136<p>Use <em>qmake</em> as before to generate a makefile. If you rename <em>main.cpp</em> temporarily, you will see the message and <em>qmake</em> will stop processing.</p>
137<h3><a name="6"></a>Checking for more than one condition</h3>
138<p>Suppose you use Windows and you want to be able to see the qDebug() statements when you run your application on the command line. Unless you build your application with the console setting, you won't see the output. We can easily put <em>console</em> on the CONFIG line so that on Windows the makefile will have this setting. But let's say that we only want to add the CONFIG line if we are running on Windows <em>and</em> when <em>debug</em> is already on the CONFIG line. This requires using two nested scopes; just create one scope, then create the other inside that one. Put the settings to be processed inside the last scope, like this:</p>
139<pre>
140 win32 {
141 debug {
142 CONFIG += console
143 }
144 }
145</pre>
146<p>Nested scopes can be joined together using colons, so the final project file looks like this:</p>
147<pre>
148 CONFIG += qt debug
149 HEADERS += hello.h
150 SOURCES += hello.cpp
151 SOURCES += main.cpp
152 win32 {
153 SOURCES += hellowin.cpp
154 }
155 unix {
156 SOURCES += hellounix.cpp
157 }
158 !exists( main.cpp ) {
159 error( "No main.cpp file found" )
160 }
161 win32:debug {
162 CONFIG += console
163 }
164</pre>
165<p>That's it! You have now completed the tutorial for <em>qmake</em>, and are ready to write project files for your development projects.</p>
166<!-- eof -->
167<p align="right">[<a href="qmake-manual-3.html">Prev: The 10 minute guide to using qmake</a>] [<a href="qmake-manual.html">Home</a>] [<a href="qmake-manual-5.html">Next: qmake Concepts</a>]</p>
168<p><address><hr><div align=center>
169<table width=100% cellspacing=0 border=0><tr>
170<td>Copyright &copy; 2007
171<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
172<td align=right><div align=right>Qt 3.3.8</div>
173</table></div></address></body>
174</html>
Note: See TracBrowser for help on using the repository browser.