Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 29

qtbase5-doc-5.12.6-2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qmake-manual.qdoc -->
  <title>Getting Started | qmake Manual</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qmake-manual.html">qmake Manual</a></td><td >Getting Started</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qmake-manual.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="prev" href="qmake-overview.html" />
  <link rel="next" href="qmake-project-files.html" />
<p class="naviNextPrevious headerNavi">
<a class="prevPage" href="qmake-overview.html">Overview</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qmake-project-files.html">Creating Project Files</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#starting-off-simple">Starting Off Simple</a></li>
<li class="level1"><a href="#making-an-application-debuggable">Making an Application Debuggable</a></li>
<li class="level1"><a href="#adding-platform-specific-source-files">Adding Platform-Specific Source Files</a></li>
<li class="level1"><a href="#stopping-qmake-if-a-file-does-not-exist">Stopping qmake If a File Does Not Exist</a></li>
<li class="level1"><a href="#checking-for-more-than-one-condition">Checking for More than One Condition</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Getting Started</h1>
<span class="subtitle"></span>
<!-- $$$qmake-tutorial.html-description -->
<div class="descr"> <a name="details"></a>
<p>This tutorial teaches you the basics of qmake. The other topics in this manual contain more detailed information about using qmake.</p>
<a name="starting-off-simple"></a>
<h2 id="starting-off-simple">Starting Off Simple</h2>
<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>hello.cpp</li>
<li>hello.h</li>
<li>main.cpp</li>
</ul>
<p>You will find these files in the <code>examples/qmake/tutorial</code> directory of the Qt distribution. 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 <code>hello.pro</code> in <code>examples/qmake/tutorial</code>. The first thing you need to do is add the lines that tell qmake 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 <a href="qmake-variable-reference.html#sources">SOURCES</a> variable. Just start a new line with <code>SOURCES +=</code> and put hello.cpp after it. You should have something like this:</p>
<pre class="cpp">

  SOURCES += hello.cpp

</pre>
<p>We repeat this for each source file in the project, until we end up with the following:</p>
<pre class="cpp">

  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 class="cpp">

  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 we use is <a href="qmake-variable-reference.html#headers">HEADERS</a>.</p>
<p>Once you have done this, your project file should look something like this:</p>
<pre class="cpp">

  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp

</pre>
<p>The target name is set automatically. It is the same as the project filename, but with the suffix appropriate for the platform. For example, if the project file is called <code>hello.pro</code>, the target will be <code>hello.exe</code> on Windows and <code>hello</code> on Unix. If you want to use a different name you can set it in the project file:</p>
<pre class="cpp">

  TARGET = helloworld

</pre>
<p>The finished project file should look like this:</p>
<pre class="cpp">

  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp

</pre>
<p>You can now use qmake to generate a Makefile for your application. On the command line, in your project directory, type the following:</p>
<pre class="cpp">

  qmake -o Makefile hello.pro

</pre>
<p>Then type <code>make</code> or <code>nmake</code> depending on the compiler you use.</p>
<p>For Visual Studio users, qmake can also generate Visual Studio project files. For example:</p>
<pre class="cpp">

  qmake -tp vc hello.pro

</pre>
<a name="making-an-application-debuggable"></a>
<h2 id="making-an-application-debuggable">Making an Application Debuggable</h2>
<p>The release version of an application does not contain any debugging symbols or other debugging 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 <code>debug</code> to the <a href="qmake-variable-reference.html#config">CONFIG</a> variable in the project file.</p>
<p>For example:</p>
<pre class="cpp">

  CONFIG += debug
  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp

</pre>
<p>Use qmake as before to generate a Makefile. You will now obtain useful information about your application when running it in a debugging environment.</p>
<a name="adding-platform-specific-source-files"></a>
<h2 id="adding-platform-specific-source-files">Adding Platform-Specific Source Files</h2>
<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: <code>hellowin.cpp</code> and <code>hellounix.cpp</code>. We cannot just add these to the <code>SOURCES</code> variable since that would place 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 we are building for.</p>
<p>A simple scope that adds the platform-dependent file for Windows looks like this:</p>
<pre class="cpp">

  win32 {
      SOURCES += hellowin.cpp
  }

</pre>
<p>When building for Windows, qmake adds <code>hellowin.cpp</code> to the list of source files. When building for any other platform, qmake simply ignores it. Now all that is left to be done is to create a scope for the Unix-specific file.</p>
<p>When you have done that, your project file should look something like this:</p>
<pre class="cpp">

  CONFIG += debug
  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp
  win32 {
      SOURCES += hellowin.cpp
  }
  unix {
      SOURCES += hellounix.cpp
  }

</pre>
<p>Use qmake as before to generate a Makefile.</p>
<a name="stopping-qmake-if-a-file-does-not-exist"></a>
<h2 id="stopping-qmake-if-a-file-does-not-exist">Stopping qmake If a File Does Not Exist</h2>
<p>You may not want to create a Makefile if a certain file does not exist. We can check if a file exists by using the <a href="qmake-test-function-reference.html#exists-filename">exists()</a> function. We can stop qmake from processing by using the <a href="qmake-test-function-reference.html#error-string">error()</a> function. This works in the same way as scopes do. Simply replace the scope condition with the function. A check for a file called main.cpp looks like this:</p>
<pre class="cpp">

  !exists( main.cpp ) {
      error( &quot;No main.cpp file found&quot; )
  }

</pre>
<p>The <code>!</code> symbol is used to negate the test. That is, <code>exists( main.cpp )</code> is true if the file exists, and <code>!exists( main.cpp )</code> is true if the file does not exist.</p>
<pre class="cpp">

  CONFIG += debug
  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp
  win32 {
      SOURCES += hellowin.cpp
  }
  unix {
      SOURCES += hellounix.cpp
  }
  !exists( main.cpp ) {
      error( &quot;No main.cpp file found&quot; )
  }

</pre>
<p>Use qmake as before to generate a makefile. If you rename <code>main.cpp</code> temporarily, you will see the message and qmake will stop processing.</p>
<a name="checking-for-more-than-one-condition"></a>
<h2 id="checking-for-more-than-one-condition">Checking for More than One Condition</h2>
<p>Suppose you use Windows and you want to be able to see statement output with <code>qDebug()</code> when you run your application on the command line. To see the output, you must build your application with the appropriate console setting. We can easily put <code>console</code> on the <code>CONFIG</code> line to include this setting in the Makefile on Windows. However, let's say that we only want to add the <code>CONFIG</code> line when we are running on Windows <i>and</i> when <code>debug</code> is already on the <code>CONFIG</code> line. This requires using two nested scopes. First create one scope, then create the other inside it. Put the settings to be processed inside the second scope, like this:</p>
<pre class="cpp">

  win32 {
      debug {
          CONFIG += console
      }
  }

</pre>
<p>Nested scopes can be joined together using colons, so the final project file looks like this:</p>
<pre class="cpp">

  CONFIG += debug
  HEADERS += hello.h
  SOURCES += hello.cpp
  SOURCES += main.cpp
  win32 {
      SOURCES += hellowin.cpp
  }
  unix {
      SOURCES += hellounix.cpp
  }
  !exists( main.cpp ) {
      error( &quot;No main.cpp file found&quot; )
  }
  win32:debug {
      CONFIG += console
  }

</pre>
<p>That's it! You have now completed the tutorial for qmake, and are ready to write project files for your development projects.</p>
</div>
<!-- @@@qmake-tutorial.html -->
<p class="naviNextPrevious footerNavi">
<a class="prevPage" href="qmake-overview.html">Overview</a>
<span class="naviSeparator">  &#9702;  </span>
<a class="nextPage" href="qmake-project-files.html">Creating Project Files</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>