The Irish Penguin Watching Open Source unfold across Ireland

1Jul/0713

QObject, QMake and Sadness – ‘Undefined reference to vtable’

Mmmm... Bit of a longwinded title there! Basically, I could not shake off the following error recently, when writing a class Browser which inherited from QObject. Apparently, the answer is that sometimes you simply have to delete your Makefiles and regenerate them using QMake (KDevelop automatically creates Makefiles from me if none are found), in order to get over this error.

/path/to/frogface/trunk/src/browser.cpp:38:
undefined reference to `vtable for Browser'
browser.o: In function `Browser':
/path/to/frogface/trunk/src/browser.cpp:34:
undefined reference to `vtable for Browser'
/path/to/frogface/trunk/src/browser.cpp:34:
undefined reference to `vtable for Browser'
collect2: ld returned 1 exit status
gmake[1]: *** [../bin/frogface] Error 1
gmake: *** [sub-src-make_default] Error 2
*** Exited with status: 2 ***

Thanks to the patient folks on the QT mailing for the help!

Funnily enough as I write this, someone's just posted the following checklist on the #qt irc channel, which is good advice

  1. Make sure the Q_OBJECT macro is present in the definition of all QObject-derived classes
  2. Make sure you define your QObject-derived classes in your header files ONLY
  3. Make sure all of your header files are listed in your .pro file in the HEADERS= list
  4. Run qmake every time you add Q_OBJECT to one of your classes or modify your .pro file
  5. The last one is particularly important as I've made an art form out of forgetting it :)

Comments (13) Trackbacks (1)
  1. Thank you, thank you, thank you! I was about ready to give up in disgust when I read your line about adding HEADERS= to the .pro file. Could I find this on Trolltech’s site? I could not.

  2. I had a similar issue, but am not using qmake in my build process.

    Instread, I resolved the issue by #include-ing the moc-generated file (input: Abc.h; output: moc_Abc.cpp) in the implementation file (moc_Abc.cpp). Not sure if this is obvious, but I didn’t see it in the primary Trolltech Qt tutorial example I have been reading.

    Thanks for the entry.

  3. Thank you very much you saved my day. Actually this is the answer to mine problems with compiling qt in Code::Blocks.

  4. Thanks for this post! I was completely baffled until I realized I never added my header file to the list in the project file. Very helpful!

  5. Edit the .pro file: Just add a space into it and then delete the space.That is OK.
    Oh…………That is really crazy.Nokia should fix this fuc* bug ASAP.

  6. Thanks! Every time I run into this issue (it happened a few times) your “checklist” comes to help… :)

  7. Really… Three years after your posting and it is still helping…. Thanks for your posting!

  8. An important note for those who still have banging their heads on the wall with this error:


    Do NOT, I repeat, DO *NOT* ADD THE Q_OBJECT MACRO to classes that don’t (yet) use signals and slots, even if they derive from QObject! (This can happen if your project is in the early stages of development, where you don’t define the signals and slots for a class, but you plan to add them later).

    Some files produced the “no relevant classes found, no code generated”, and when the vtable was not found (because no signals-and-slots code was generated), I got the link error.

    After removing the Q_OBJECT macro from said classes, everything ran fine – even if they were QObject descendants. But the tricky thing was when I had included the definition for MORE THAN ONE class in a header file, and running moc DID produce some code, but not for ALL classes! (Also I didn’t use the standard qmake because I was developing the project in Code::Blocks IDE, and had to run moc for every file separately, so it was a quite particular case).

    After removing the Q_OBJECT macro from the offending “incomplete” classes (without signals/slots), compilation proceeded correctly.

    If you add this warning to your 4-checklist, all the frustrated qt programmers will thank you :)

  9. Thanks, good advice!

  10. For read QT surfer, i get supplementary information. Order in pro file is play important role. For get it, i loose 3 fucken hours, looking on ‘undefined reference to `vtable blablalba… ‘ . I hope that will be helpful for someone, who will have the same situation.

    peace!

  11. I speak about HEADERS and SOURCES variables which includes src files, if someone not get it.

  12. Thanks!!! Save me a lot of time.. Happens from time to time with QT creator

  13. Finally someone who knows what their talking about. Those steps are awesome. Thank you!


Leave a comment

(required)