jump to navigation

“undefined reference to `vtable for …’ errors” in Qt derived classes April 24, 2009

Posted by lizardo in Hints.
Tags:
comments closed

If you ever come across an error like this when compiling C++ code with Qt derived class definitions:


g++ -Wl,--no-undefined -o test test.o -L/usr/lib -lQtGui -lQtCore -lpthread
test.o: In function `main':
test.cpp:(.text+0x2c): undefined reference to `vtable for MyClass'
collect2: ld returned 1 exit status
make: *** [test] Error 1

It is probably because either you defined a class in a .cpp file or you forgot to add some header file to the HEADERS variable in the .pro file.

This happens because moc (Qt’s meta-object compiler) only runs on header files by default (and only on those listed in the HEADERS variable), therefore it did not generate the necessary MOC code for that class.

The fix is to simply move the class definition to a header, and make sure it is added to the HEADERS variable in the qmake project file.