The header files, or any other file that contains some of the Qt-specific macros, need to be processed by moc, the Meta-Object Compiler, before they can be compiled by g++.
So let's assume we modify the code from above as follows. First, we create a file called ``main.cpp'':
#include <qapplication.h>
#include "myButton.h"
int main( int argc, char **argv )
{
QApplication a( argc, argv );
myButton button( "Hello World!");
a.setMainWidget( &button );
button.show();
return a.exec();
}
Here, we are making use of a class called ``myButton'', which is defined in the header ``myButton.h'':
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <qpushbutton.h>
class myButton : public QPushButton
{
Q_OBJECT
public:
myButton(const QString& text, QWidget *parent=0);
};
#endif
The actual code for all of ``myButton'''s functionality goes into ``myButton.cpp'':
#include "myButton.h"
myButton::myButton(const QString& text, QWidget *parent)
: QPushButton(text, parent)
{
resize( 100, 30 );
connect(this, SIGNAL(clicked()), this, SLOT(close()));
}
Granted, this does not do very much and could easily be achieved in our first example in a much more obvious way. However, let's take this example to show how to compile a program that consists of several files. Now we have:
The first step is to compile the .cpp files, which is identical to the commands used above:
$> g++ -Wall -I$QTDIR/include -c myButton.cpp
$> g++ -Wall -I$QTDIR/include -c main.cpp
Now, before we can continue and link the object files together, we need to use the meta-object compiler to process the header files:
$> moc myButton.h -o myButton.moc.cpp
This creates a new .cpp file, which in turn we need to compile into an object file:
$> g++ -Wall -I$QTDIR/include -c myButton.moc.cpp
Finally, we have all our object files and continue to link the application similar to our first example:
$> g++ -Wall -L$QTDIR/lib myButton.o main.o myButton.moc.o -lqt -o hello
HEADERS = myButton.h SOURCES = myButton.cpp main.cpp TARGET = hello
Run tmake and make just as in the example above:
$> tmake hello.pro -o Makefile
$> make
And once again: voilà!