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

« back to all changes in this revision

Viewing changes to doc/html/qmake-tutorial.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: qmake Tutorial</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
    <link rel="prev" href="qmake-advanced-usage.html" />
 
15
    <link rel="contents" href="qmake-manual.html" />
 
16
    <link rel="next" href="qmake-common-projects.html" />
 
17
</head>
 
18
<body>
 
19
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
20
<tr>
 
21
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
22
<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>
 
23
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><p>
 
24
[Previous: <a href="qmake-advanced-usage.html">qmake Advanced Usage</a>]
 
25
[<a href="qmake-manual.html">Contents</a>]
 
26
[Next: <a href="qmake-common-projects.html">qmake Common Projects</a>]
 
27
</p>
 
28
<h1 align="center">qmake Tutorial</h1>
 
29
<p>This tutorial teaches you how to use <tt>qmake</tt>. We recommend that you read the <tt>qmake</tt> user guide after completing this tutorial.</p>
 
30
<a name="starting-off-simple"></a>
 
31
<h2>Starting off Simple</h2>
 
32
<p>Let's assume that you have just finished a basic implementation of your application, and you have created the following files:</p>
 
33
<ul>
 
34
<li>hello.cpp</li>
 
35
<li>hello.h</li>
 
36
<li>main.cpp</li>
 
37
</ul>
 
38
<p>You will find these files in <tt>qt/qmake/examples/tutorial</tt>. 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 <tt>hello.pro</tt> in <tt>qt/qmake/tutorial</tt>. The first thing you need to do is add the lines that tell <tt>qmake</tt> about the source and header files that are part of your development project.</p>
 
39
<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 <tt>SOURCES +=</tt> and put hello.cpp after it. You should have something like:</p>
 
40
<pre>&nbsp;   SOURCES += hello.cpp</pre>
 
41
<p>We repeat this for each source file in the project, until we end up with:</p>
 
42
<pre>&nbsp;   SOURCES += hello.cpp
 
43
    SOURCES += main.cpp</pre>
 
44
<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>
 
45
<pre>&nbsp;   SOURCES = hello.cpp \
 
46
              main.cpp</pre>
 
47
<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>
 
48
<p>Once you have done this, your project file should look something like this:</p>
 
49
<pre>&nbsp;   HEADERS += hello.h
 
50
    SOURCES += hello.cpp
 
51
    SOURCES += main.cpp</pre>
 
52
<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 <tt>hello.pro</tt>, the target will be <tt>hello.exe</tt> on Windows and <tt>hello</tt> on Unix. If you want to use a different name you can set it in the project file:</p>
 
53
<pre>&nbsp;   TARGET = helloworld</pre>
 
54
<p>The final step is to set the <tt>CONFIG</tt> variable. Since this is a Qt application, we need to put <tt>qt</tt> on the CONFIG line so that <tt>qmake</tt> will add the relevant libraries to be linked against and ensure that build lines for <tt>moc</tt> and <tt>uic</tt> are included in the makefile.</p>
 
55
<p>The finished project file should look like this:</p>
 
56
<pre>&nbsp;   CONFIG += qt
 
57
    HEADERS += hello.h
 
58
    SOURCES += hello.cpp
 
59
    SOURCES += main.cpp</pre>
 
60
<p>You can now use <tt>qmake</tt> to generate a makefile for your application. On the command line, in your application directory, type:</p>
 
61
<pre>&nbsp;   qmake -o Makefile hello.pro</pre>
 
62
<p>Then type <tt>make</tt> or <tt>nmake</tt> depending on the compiler you use.</p>
 
63
<p>For Visual Studio users, <tt>qmake</tt> can also generate <tt>.dsp</tt> or <tt>.vcproj</tt> files, for example:</p>
 
64
<pre>&nbsp;   qmake -tp vc -o hello.dsp hello.pro</pre>
 
65
<a name="making-an-application-debuggable"></a>
 
66
<h2>Making an Application Debuggable</h2>
 
67
<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 <tt>debug</tt> to the CONFIG variable in the project file.</p>
 
68
<p>For example:</p>
 
69
<pre>&nbsp;   CONFIG += qt debug
 
70
    HEADERS += hello.h
 
71
    SOURCES += hello.cpp
 
72
    SOURCES += main.cpp</pre>
 
73
<p>Use <tt>qmake</tt> as before to generate a makefile and you will be able to debug your application.</p>
 
74
<a name="adding-platform-specific-source-files"></a>
 
75
<h2>Adding Platform-Specific Source Files</h2>
 
76
<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 - <tt>hellowin.cpp</tt> and <tt>hellounix.cpp</tt>. We can't just add these to the <tt>SOURCES</tt> 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 <tt>qmake</tt> is run on.</p>
 
77
<p>A simple scope which will add in the platform dependent file for Windows looks like this:</p>
 
78
<pre>&nbsp;   win32 {
 
79
        SOURCES += hellowin.cpp
 
80
    }</pre>
 
81
<p>So if <tt>qmake</tt> is run on Windows, it will add <tt>hellowin.cpp</tt> to the list of source files. If <tt>qmake</tt> 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>
 
82
<p>When you have done that, your project file should now look something like this:</p>
 
83
<pre>&nbsp;   CONFIG += qt debug
 
84
    HEADERS += hello.h
 
85
    SOURCES += hello.cpp
 
86
    SOURCES += main.cpp
 
87
    win32 {
 
88
        SOURCES += hellowin.cpp
 
89
    }
 
90
    unix {
 
91
        SOURCES += hellounix.cpp
 
92
    }</pre>
 
93
<p>Use <tt>qmake</tt> as before to generate a makefile.</p>
 
94
<a name="stopping-qmake-if-a-file-doesn-t-exist"></a>
 
95
<h2>Stopping qmake If a File Doesn't Exist</h2>
 
96
<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 <tt>qmake</tt> 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>
 
97
<pre>&nbsp;   !exists( main.cpp ) {
 
98
        error( &quot;No main.cpp file found&quot; )
 
99
    }</pre>
 
100
<p>The &quot;!&quot; 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>
 
101
<pre>&nbsp;   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
    !exists( main.cpp ) {
 
112
        error( &quot;No main.cpp file found&quot; )
 
113
    }</pre>
 
114
<p>Use <tt>qmake</tt> as before to generate a makefile. If you rename <tt>main.cpp</tt> temporarily, you will see the message and <tt>qmake</tt> will stop processing.</p>
 
115
<a name="checking-for-more-than-one-condition"></a>
 
116
<h2>Checking for More than One Condition</h2>
 
117
<p>Suppose you use Windows and you want to be able to see the <a href="qtglobal.html#qDebug">qDebug</a>() 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 <tt>console</tt> 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 <tt>and</tt> when <tt>debug</tt> 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>
 
118
<pre>&nbsp;   win32 {
 
119
        debug {
 
120
            CONFIG += console
 
121
        }
 
122
    }</pre>
 
123
<p>Nested scopes can be joined together using colons, so the final project file looks like this:</p>
 
124
<pre>&nbsp;   CONFIG += qt debug
 
125
    HEADERS += hello.h
 
126
    SOURCES += hello.cpp
 
127
    SOURCES += main.cpp
 
128
    win32 {
 
129
        SOURCES += hellowin.cpp
 
130
    }
 
131
    unix {
 
132
        SOURCES += hellounix.cpp
 
133
    }
 
134
    !exists( main.cpp ) {
 
135
        error( &quot;No main.cpp file found&quot; )
 
136
    }
 
137
    win32:debug {
 
138
        CONFIG += console
 
139
    }</pre>
 
140
<p>That's it! You have now completed the tutorial for <tt>qmake</tt>, and are ready to write project files for your development projects.</p>
 
141
<p>
 
142
[Previous: <a href="qmake-advanced-usage.html">qmake Advanced Usage</a>]
 
143
[<a href="qmake-manual.html">Contents</a>]
 
144
[Next: <a href="qmake-common-projects.html">qmake Common Projects</a>]
 
145
</p>
 
146
<p /><address><hr /><div align="center">
 
147
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
148
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
149
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
150
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
151
</tr></table></div></address></body>
 
152
</html>