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
|
<!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-tutorial.leaf:3 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>qmake Tutorial</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 Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped 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-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>
<h2 align="center">qmake Tutorial</h2>
<h3><a name="1"></a>Introduction to the qmake tutorial</h3>
<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>
<h3><a name="2"></a>Starting off simple</h3>
<p>Let's assume that you have just finished a basic implementation of your application, and you have created the following files:</p>
<ul><li><p>hello.cpp</p>
<li><p>hello.h</p>
<li><p>main.cpp</p>
</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>
<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>
<pre>
SOURCES += hello.cpp
</pre>
<p>We repeat this for each source file in the project, until we end up with:</p>
<pre>
SOURCES += hello.cpp
SOURCES += main.cpp
</pre>
<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>
<pre>
SOURCES = hello.cpp \
main.cpp
</pre>
<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>
<p>Once you have done this, your project file should look something like this:</p>
<pre>
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
</pre>
<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>
<pre>
TARGET = helloworld
</pre>
<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>
<p>The finished project file should look like this:</p>
<pre>
CONFIG += qt
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
</pre>
<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>
<pre>
qmake -o Makefile hello.pro
</pre>
<p>Then type <em>make</em> or <em>nmake</em> depending on the compiler you use.</p>
<h3><a name="3"></a>Making an application debuggable</h3>
<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>
<p>For example:</p>
<pre>
CONFIG += qt debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
</pre>
<p>Use <em>qmake</em> as before to generate a makefile and you will be able to debug your application.</p>
<h3><a name="4"></a>Adding platform specific source files</h3>
<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>
<p>A simple scope which will add in the platform dependent file for Windows looks like this:</p>
<pre>
win32 {
SOURCES += hellowin.cpp
}
</pre>
<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>
<p>When you have done that, your project file should now look something like this:</p>
<pre>
CONFIG += qt debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
win32 {
SOURCES += hellowin.cpp
}
unix {
SOURCES += hellounix.cpp
}
</pre>
<p>Use <em>qmake</em> as before to generate a makefile.</p>
<h3><a name="5"></a>Stopping qmake if a file doesn't exist</h3>
<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>
<pre>
!exists( main.cpp ) {
error( "No main.cpp file found" )
}
</pre>
<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>
<pre>
CONFIG += qt debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
win32 {
SOURCES += hellowin.cpp
}
unix {
SOURCES += hellounix.cpp
}
!exists( main.cpp ) {
error( "No main.cpp file found" )
}
</pre>
<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>
<h3><a name="6"></a>Checking for more than one condition</h3>
<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>
<pre>
win32 {
debug {
CONFIG += console
}
}
</pre>
<p>Nested scopes can be joined together using colons, so the final project file looks like this:</p>
<pre>
CONFIG += qt debug
HEADERS += hello.h
SOURCES += hello.cpp
SOURCES += main.cpp
win32 {
SOURCES += hellowin.cpp
}
unix {
SOURCES += hellounix.cpp
}
!exists( main.cpp ) {
error( "No main.cpp file found" )
}
win32:debug {
CONFIG += console
}
</pre>
<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>
<!-- eof -->
<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>
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright © 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>
|