PDA

View Full Version : Strange linker error - can anyone help? (Qt 4.1.2)



Dr. Norbert Prang
16th June 2006, 13:39
Hello,

is there anybody, who can explain to me the cause for the following strange linker error in my Xcode based Qt project?

/usr/bin/ld: Undefined symbols:
vtable for GoButton
/Users/Norbert/MyCode/SlotTest01/build/SlotTest01.build/Debug/SlotTest01.build/Objects-normal/ppc/main.o reference to undefined vtable for GoButton
collect2: ld returned 1 exit status
/usr/bin/ld: Undefined symbols:
vtable for GoButton
/Users/Norbert/MyCode/SlotTest01/build/SlotTest01.build/Debug/SlotTest01.build/Objects-normal/ppc/main.o reference to undefined vtable for GoButton
collect2: ld returned 1 exit status


For better understanding:

I intended to make just a simple program for testing selfmade slots. The code of the main.cpp ist the following:

#include <qapplication.h>
#include <qpushbutton.h>
#include <qobject.h>
#include <qstring.h>
#include <qwidget.h>

#include <unistd.h>

class GoButton : public QPushButton
{
Q_OBJECT
public:
GoButton(const QString& text, QWidget* parent)
: QPushButton(text, parent)
{
connect(this, SIGNAL(clicked()), this, SLOT(go()));
}
public slots:
void go()
{
static int count = 0;
count++;

if(count > 5)
{
qApp->quit(); //Finish after 5th click
}

}

};

int main(int argc, char** argv)
{
QApplication a(argc, argv);

GoButton* b = new GoButton("Press Me, Please!", 0);

b->show();

QObject::connect(qApp, SIGNAL(lastWindowClosed()), qApp, SLOT(quit()));

return a.exec();
}



I made a Xcode project by processing the main.cpp as the only file in my project directory as follows:

I typed: qmake –project

The resulting Qt .pro file looks as follows:

################################################## ####################
# Automatically generated by qmake (2.00a) Fri Jun 16 13:07:34 2006
################################################## ####################

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

# Input
SOURCES += main.cpp

Then I typed: qmake

Then I opened the Xcode project and started a build process. All compiler action seemed to be o. k..The linker error above occured.


I cannot explain to me, what could be wrong here.

Can anyone? I hope ...

Have a nice weekend, you all!

Norbert

jacek
16th June 2006, 14:14
You must run moc utility on every class that contains Q_OBJECT macro to generate missing code. Usually qmake does this for you, but unfortunately it only checks header files for Q_OBJECT, so moc won't be run if you have a class definition inside a .cpp file.

The easiest solution is to add #include "main.moc" at the end of main.cpp file (don't forget to rerun qmake). This tells qmake that it has to run moc on given file.

Dr. Norbert Prang
16th June 2006, 14:55
Hi,

thanks for quick and useful reply! That works really phantastic. Thanks again.

Have a nice weekend.
Norbert :)