Next: , Previous: Vala Support, Up: Programs


8.17 Qt Support

Automake includes initial support for the Qt library (http://www.qtsoftware.com/products/). This includes calling the Meta Object Compiler (MOC) and compiling/linking the resulting C++ files to the executable (or library). Support for calling the other Qt tools (uic, rcc, lconvert) is not included (yet).

The shipped AM_PROG_MOC macro should be used to look for the MOC tool on the build system. For checking the existence of the Qt library and different parts of it, you can use pkg-config.

— Macro: AM_PROG_MOC ([MINIMUM-VERSION])

Tries to find Qt's Meta Object Compiler and if found the variable MOC will be set. The value of this variable will be used during the compilation to generate meta object code. Optionally you can give a minimum version which should be checked for. The default is 58, that version was shipped with Qt 4.0.1.

With Qt's Meta Object Compiler C++ source can be generated from headers and sources. Generated source from a header file has to be compiled and then linked to the binary. Header files which should be run through the Meta Object Compiler have to be listed in the prog_QTSOURCES variable (and in prog_SOURCES too). The generated C++ files will be named according to Qt customs (moc_foo.cpp).

A working Makefile.am example:

     bin_PROGRAMS = cutegammon
     
     cutegammon_QTSOURCES = board.h checker.h clickablelabel.h             \
             diceanimation.h dice.h doubledice.h lineedit.h mainwindow.h   \
             point.h spot.h
     
     cutegammon_SOURCES = board.cpp checker.cpp clickablelabel.cpp   \
             diceanimation.cpp dice.cpp doubledice.cpp lineedit.cpp  \
             main.cpp mainwindow.cpp point.cpp spot.cpp              \
             $(cutegammon_QTSOURCES)
     
     AM_CXXFLAGS = `pkg-config --cflags QtGui`
     AM_LDFLAGS = `pkg-config --libs QtGui`

The configure.ac for this project:

     AC_INIT([cutegammon], [0.1])
     AM_INIT_AUTOMAKE([-Wall foreign])
     
     AC_PROG_CXX
     AM_PROG_MOC
     
     AC_CONFIG_FILES([Makefile])
     AC_OUTPUT

In some interesting situations it may be needed to generate meta object code from C++ source files. The usual way to handle this in the Qt world is to generate meta object code in a file (named foo.moc for foo.cpp) and include that at the end of the original source:

     #include <QObject>
     #include <QWidget>
     
     class InCodeClass : public QObject {
       Q_OBJECT
     ...
     };
     
     #include <incodeclass.moc>

C++ source files like this have to be added to prog_QTSOURCES too. The generated foo.moc won't be compiled and linked of course, since its code will be generated together with the foo.cpp. An explicit dependency for foo.o to foo.moc is recorded, so make will first create the moc file and only after that will try to compile the whole foo.cpp with the C++ compiler.

Please note that in the example we included cutegammon_QTSOURCES to cutegammon_SOURCES, this way it can be avoided to repeat the same source file names again, which would be quite error prone.