Application Development with Qt Creator(Second Edition)
上QQ阅读APP看书,第一时间看更新

Learning the landscape – the Build menu and the .pro files

In the previous chapter, you learned how to build applications by hitting the hammer button in the corner of Qt Creator's main window or by starting the debugger. To just build your library—or any application—you can either use the hammer icon or the various choices in the Build menu. The obvious choice is either Build All or Rebuild All; choosing Build All recompiles only those files that need to be rebuilt as recognized by Qt Creator; Rebuild All cleans the project of all the object files and rebuilds the entire project from scratch. In most cases, it's sufficient to choose Build All, and that's what you want to do because it's faster. Sometimes, you really do want to rebuild the whole project, when things are broken and Qt's Make system can't reconcile all the dependencies (or you've incorrectly specified them). Select Build All now and wait for it to build while we discuss the other options.

The Build menu lets you build a single file—it is handy if all you want to do is check the syntax of the code you're writing and to make sure that it is free of errors—or the entire project. It also lets you run the project outside the debugger, which you might want to do in some circumstances, such as when giving a demonstration. You can also clean your project (remove all object files and other autogenerated products) by selecting Clean All.

The Publish option is available for some add-on kits that let you publish compiled applications and libraries to application stores and repositories; you can find more details about this in the documentation for any Qt Creator add-in, such as the SDKs for Maemo development (an older Linux variant from Nokia for handheld devices).

Behind every Qt Creator project is a .pro file; this serves the same function as a Makefile, and in fact, it is processed by a Qt toolkit command called qmake.

Note

A Makefile is a file that describes how your application can be built using the utility make. For more information, go to http://en.wikipedia.org/wiki/Make_(software). Qt provides qmake, a utility that converts the .pro files to Makefiles; you'll work with the Qt Creator GUI most of the time to create the .pro files and ignore the resulting Makefile.

These files are declarative; in that, you declare the relationships between the files that make up your application, and qmake figures out how to build your application from there. In most cases, you'll need to make a few or no changes to a .pro file, but it doesn't hurt to understand how they work. Double-click on MathFunctions.pro and you'll find this:

#-------------------------------------------------
#
# Project created by QtCreator 2013-07-23T19:50:46
#
#-------------------------------------------------

QT       -= gui

TARGET = MathFunctions
TEMPLATE = lib
CONFIG += staticlib

SOURCES += mathfunctions.cpp

HEADERS += mathfunctions.h
unix {
    target.path = /usr/lib
    INSTALLS += target
}

The basic syntax of a .pro file is variable assignments; the file generated by Qt Creator assigns the following variables:

  • QT: This variable indicates the Qt modules that your project will link against. By default, all projects include QtCore and QtGui; there's a plethora of other modules available, which include key features such as the WebKit web browsing engine (QtWebkit) and multimedia libraries (Phonon). Our assignment here indicates that we use the default Qt modules but don't link them against QtGui.
  • TARGET: This variable is the name of the compiled library or executable.
  • TEMPLATE: This variable indicates the kind of template that qmake should use to generate the binary; in our case, we're saying that it should use the template to create a lib file—a static library.
  • CONFIG: This variable passes an additional configuration to qmake's template; here, we say that we want a statically linked library.
  • SOURCES and HEADERS: These variables contain lists of the source and header files that make up our project.
  • INSTALLS: This variable indicates where the resulting build product should be installed. Here, it's set in a scope. Scopes let you specify conditional options in qmake; the condition for the scope is a variable or an expression, which might be true or false, and the code that follows is executed if the variable is true. The scope at the end of this file says, "if we're building for a Unix variant, set the target.path variable to /usr/lib and the INSTALLS variable to target."

These are the basic variables you'll find in almost any .pro file.

Note

For a good discussion of qmake scopes that you can use to control conditional compilation, see http://qt-project.org/doc/qt-4.8/qmake-advanced-usage.html.

Two additional variables that you're likely to want to know about are DEFINES and LIBS; DEFINES lets you specify preprocessor defines that should be set throughout the build process, and LIBS indicates additional libraries against which Qt Creator should link your project.

Note how variables are managed: you use = for assignment, += to add an item to a list, and -= to remove an item from a list.