“undefined reference to `vtable for …’ errors” in Qt derived classes

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.

About lizardo

My hobby: figure out how systems are expected to work; induce them to work unexpectedly; and responsibly disclose.
This entry was posted in Tricks and tagged . Bookmark the permalink.

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

  1. … 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.

  2. Arun says:

    I had similar problem. I added the header files and cpp files in the project file and there was no error.

    • Carp says:

      I had to change
      HEADERS += \
      HEADERS += window.h

      now it works

      • Chris Lee says:

        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

  3. Steve says:

    Thanks!! Problem resolved. Even qt examples need to be separated. The clientserver example was giving the same error when compiled as it is.

  4. Jocke Saxin says:

    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.

  5. Sunil says:

    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.


  6. Darmawan says:

    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.

  7. spandan says:

    “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

  8. Priju says:

    thank you..that was indeed great.

  9. Pingback: My blog in 2010 « Anderson Lizardo's Blog

  10. carlosdlg says:

    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.

  11. Gary S. says:

    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.

  12. Tom says:

    Thanks. Super helpful.

  13. Dan says:

    Thank you!
    It’s working.

  14. Mike says:

    or else, using Kdevelop4 type:
    make rebuild_cache
    make all

  15. Claus Wimmer says:

    I had found the following lines in *.pro:


    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.

  16. Pingback: Qt : undefined reference to vtable for … - 30 minutes par jour

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s