Skip to main content

Integrating Qt's Meta object compiler (moc) with GNU Automake

Smoothly integrating Moc into a GNU autotools project is not trivial. What I present here is just one way, rather than "the way". Actually just a few bytes to get you started. Let's go.

File Makefile.am:

include ${top_srcdir}/moc.mk

bin_PROGRAMS = \
    automoc

$(automoc_OBJECTS) : \
  moc_qobject_class_a.cpp \
  moc_qobject_class_b.cpp

automoc_SOURCES = \
    qobject_class_a.h \
    qobject_class_b.h \
    \
    qobject_class_a.cpp \
    qobject_class_b.cpp

Since the object files of our example program automoc all depend on the moc files, make will first generate all the mocs and then start compiling.

File moc.mk:

moc_%.cpp : %.h
  $(MOC) -i -o "$@" "$<"

clean-moc-extra:
  rm -vf moc_*.cpp

clean-am: clean-moc-extra

Keep in mind we are embedding plain GNU Make rules here so true tab characters are needed before the commands. This can only work as is with the original C++ source files including the related moc source files:

File qobject_class_a.cpp:

..
#include "qobject_class_a.h"
..
..
#include "moc_qobject_class_a.cpp"

To see this solution at work have a look at the Relacs Subversion repository. As a last note don't forget to make sure that you are calling the right moc, either that of Qt3 or Qt4. Good luck!