The Meta Object Compiler, moc among friends, is the program which handles the C++ extensions in Qt.
Here are the command-line options supported by the moc:
moc is typically used with an input file containing class declarations like this skeleton:
class YourClass : public QObject { Q_OBJECT public: YourClass::YourClass( QObject * parent=0, const char * name=0 ); YourClass::~YourClass(); signals: public slots: };
Here is a useful makefile rule if you only use GNU make:
m%.cpp: %.h moc $lt; -o $@
If you want to write portably, you can use individual rules of the following form:
mNAME.cpp: NAME.h moc $< -o $@
You must also remember to add mNAME.cpp to your SOURCES (substitute your favorite name) variable and mNAME.o to your OBJECTS variable.
(While we prefer to name our C++ source files .cpp, the moc doesn't know that, so you can use .C, .cc, .CC, .cxx or even .c++ if you prefer.)
If you have class declarations in C++ files, we recommend that you use a makefile rule like this:
NAME.o: mNAME.cpp mNAME.cpp: NAME.cpp moc -i $< -o $@
This guarantees that make(1) will run the moc before it compiles NAME.cpp. You can then put
#include "nNAME.cpp"
at the end of NAME.cpp, where all the classes declared in that file are fully known.
Sometimes you may get linkage errors, saying that YourClass::className() is undefined or that YourClass lacks a vtbl. Those errors happen most often when you forget to compile the moc-generated C++ code or include that object file in the link command.
The moc will warn you about a number of dangerous or illegal constructs.
moc does not handle all of C++. The main problem is that class templates cannot have signals or slots. This is an important bug. Here is an example:
class SomeTemplate<int> : public QFrame { Q_OBJECT [...] signals: void bugInMocDetected( int ); };
Less importantly, the following five constructs are illegal. All five have workarounds which we think are better alternatives, so fixing these bugs is not a high priority for us.
In most cases where you would consider that, we think inheritance is a better alternative. Here is an example of illegal syntax:
class someClass : public QObject { Q_OBJECT [...] public slots: void apply(void (*applyFunction)(QList*, void*), char*); };
You can work around this restriction like this:
typedef void (*ApplyFunctionType)(QList*, void*); class someClass : public QObject { Q_OBJECT [...] public slots: void apply( ApplyFunctionType, char *); };
(It may sometimes be even better to replace the function pointer with inheritance and virtual functions, signals or slots.)
Sometimes it will work, but in general, friend declarations can not be placed in signals or slots sections. Put them in the good old private, protected or public sections instead. Here is an example of the illegal syntax:
class someClass : public QObject { Q_OBJECT [...] signals: friend class ClassTemplate<char>; };
The C++ feature of upgrading an inherited member function to public status is not extended to cover signals and slots. Here is an illegal example:
class Whatever : public QButtonGroup { [...] public slots: void QButtonGroup::buttonPressed; [...] };
The QButtonGroup::buttonPressed() slot is protected.
C++ quiz: What happens if you try to upgrade a protected member function which is overloaded?
This is regrettable, but the moc places more emphasis on getting modern syntax like templates and typedefs right than on anachronisms like #define. In other words, we don't like #define and don't want to fix this problem. Here is an illegal example:
#ifdef ultrix #define SIGNEDNESS(a) unsigned a #else #define SIGNEDNESS(a) a #endif class Whatever : public QObject { [...] signals: void someSignal( SIGNEDNESS(a) ); [...] };
A #define without arguments works.
It is a mystery to me why anyone would put a constructor on either the signals or slots sections. You can not, anyway (except that it happens to work in some cases). Put them in private, protected or public sections, where they belong. Here is an example of the illegal syntax:
class SomeClass : public QObject { Q_OBJECT public slots: SomeClass( QObject *parent, const char *name ) : QObject( parent, name ) {} [...] };