1.6.  qmake, Project Files, and Makefile

[ fromfile: qmake.xml id: qmake ]

Figure 1.1.  (q)make build steps

(q)make build steps

src/early-examples/example0> ls
fac.cpp
src/early-examples/example0> qmake -project
src/early-examples/example0> ls
example0.pro fac.cpp
src/early-examples/example0> cat example0.pro

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
SOURCES += fac.cpp
src/early-examples/example0> qmake
src/early-examples/example0> ls
example0.pro fac.cpp Makefile
src/early-examples/example0> make
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB
  -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I.
  -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore
  -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4
  -I. -I. -I. -o fac.o fac.cpp
g++ -o example0 fac.o  -L/usr/lib -lQtGui -lQtCore -lpthread
src/early-examples/example0> ls
example0 example0.pro fac.cpp fac.o Makefile
src/early-examples/example0>
src/early-examples/example0> ./example0
Factorial of: 10
The Factorial of 10 is: 3628800
src/early-examples/example0> ./example0
Factorial of: -3
No negative values, please!
Factorial of: 0
The Factorial of 0 is: 1
src/early-examples/example0>
 

Example 1.6. src/common.pri

# required if you want to see qDebug() messages
CONFIG += debug

# place auto-generated files in "invisible" subdirectories
win32 {
    MOC_DIR = _moc
    UI_DIR = _ui
    OBJECTS_DIR = _obj
} else {
    UI_DIR = .ui
    MOC_DIR = .moc
    OBJECTS_DIR = .obj
}

# rules below apply to TEMPLATE=app projects only:
app {
    # place executable in same folder:
    DESTDIR=$$OUT_PWD

    # don't place executables in an app bundle on mac os
    # this also permits console apps to work on the mac
    mac {
        CONFIG -= app_bundle
    }

    # Open a console for stdin, stdout, and stderr Windows:
    win32 {
        CONFIG += console
    }
}

[Note] CONFIG += console (for MSVC users)
  • CONFIG += console

    • Only needed on Windows platforms

    • Permits output to qDebug(), stdout and stderr

[Note] CONFIG -= app_bundle (for Mac OSX)
  • For console apps on the Mac, you can prevent the creation of an "app bundle" that places your executable in a subdirectory by adding the line

    CONFIG -= app_bundle

    in your project file.

  • That will also permit interactions with standard I/O.

  • You can then use the command-line

    qmake -spec macx-g++

    to produce a Makefile.

  • The command make> can then produce a single executable for your app.

  • CONFIG -= app_bundle

    • Only needed for console apps on the Mac

    • Permits interaction with stdio.

    • Prevents build from creating an "application bundle".

    • Everything ends up in a single executable.



[6] Depending on your development environment, you may find variants of make, such as mingw32-make, gmake, or cmake. In MS Dev Studio, you'll find nmake.

[7] To produce a Makefile in MacOSX, you need to add the line CONFIG -= app_bundle to the project file and you need to type qmake -spec macx-g++ instead of just qmake.