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.
… or you can add something like this (supposing that MyClass definition is under MyClass.cpp file):
#include “MyClass.moc”
At the end of MyClass.cpp file 🙂 It is very useful when you need to define private classes (see http://techbase.kde.org/Policies/Library_Code_Policy#D-Pointers ) which doesn’t need to be visible from header files.
I had similar problem. I added the header files and cpp files in the project file and there was no error.
Thanks,
I had to change
HEADERS += \
window.h
into
HEADERS += window.h
now it works
I know this is old but thanks for the reply, I was fighting till 2am with code that cometimes worked, sometimes got this terrible vtable error, changed the .pro and error went away
Thanks!! Problem resolved. Even qt examples need to be separated. The clientserver example was giving the same error when compiled as it is.
Or if you have be renaming things you just have to make distclean. I just spent an hour on this error before solving it by rerunning qmake from the terminal.
Thanks brother…rerunning qmake worked for me too… 🙂
And 10 years later this still saved me my ass and many hours, thank you so much 😀
If the Q_OBJECT macro is added to the classes at a later point, you have to manually launch “qmake” (without any parameter). Because it has to regenerate the makefile adding the calls to MOC files for the new classes.
http://forums.codeblocks.org/index.php/topic,2253.0.html
This helped me *so* much; I probably never would have thought to do this.
<3-
thanks for the details
its works for me .. thanks
Thanks a lot !
Thanks for the hint. I encountered the same error a while ago. I was using autotroll to generate the moc cpp file and forgot to add the routine to generate the moc.cpp file to Makefile.am.
“This happens because moc (Qt’s meta-object compiler) only runs on header files by default”
After hours of hunting, finally the solution..thanks man..i had the class definition within the .cpp file with no separate declaration in an exclusive header…that was the problem
thank you..that was indeed great.
Pingback: My blog in 2010 « Anderson Lizardo's Blog
Old thread, I know.
I found this error today (Qt for Symbian), and this post’s suggestion didn’t help. The class was defined in a separate header file, and all headers were correctly included in the HEADERS project variable.
The project compiled (and ran) without errors for the simulator, but when changing target to run on a device it didn’t compile.
The solution was to manually delete the make file.
It seems that it is a Qt Creator’s bug when switching targets.
I’m using Qt Creator 2.0.1
Hope this helps someone some day.
Thanks! This helped me!
My problem was that I added the Q_OBJECT macro to my .h file, but I forgot to re-run qmake. Once I ran qmake, I could link just fine.
Thanks. Super helpful.
Thank you!
It’s working.
or else, using Kdevelop4 type:
make rebuild_cache
and
make all
I had found the following lines in *.pro:
HEADERS+=…h\
…h\
cmain.h
SOURCES+=….cpp\
And I have inserted an empty line before the SOURCES+= section.
First the linker was unable to find the vtables and after the modification the linker was able to link the runnable.
Pingback: Qt : undefined reference to vtable for … - 30 minutes par jour