Thursday, 5 March 2009

How to Fix undefined reference to vtable

I was working on reorganising some code when I came across the following error which stumped me for ages. I was trying to move a class out of "Simulation.h/Simulation.cp" and into "SceneWindow.h/SceneWindow.cp".

make -k -s
Simulation.o: In function `~TSceneWindow':
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
/home/binh/Documents/polyworld/app/Simulation.cp:6392: undefined reference to `vtable for TSceneWindow'
Simulation.o:/home/binh/Documents/polyworld/app/Simulation.cp:6392: more undefined references to `vtable for TSceneWindow' follow
collect2: ld returned 1 exit status
make: *** [Polyworld] Error 1
make: Target `first' not remade because of errors.

Compilation exited abnormally with code 2 at Thu Mar  5 02:51:00

The solution was to ensure that I placed a reference to "SceneWindow.h" in my "Makefile". In my case I needed to place the reference in my ".pro" file and then use "qmake" to generate a "Makefile".

This took me hours to figure out. Grr.



9/6/2011

A few people have kindly shared their tips.

Anonymous suggested
  • running qmake and then re-building

miformaprohibidaenlibertad suggested
  • This problem is also related with virtual functions. A common reason of the "vtable" message is not declaring a virtual function equal to zero, for example virtual foo(); instead of virtual foo() = 0;

I suggest that you first make sure that you are including all the necessary header files in your Makefile. I had to do this because I was writing my Makefiles manually.

If you are using Qt, you can rely on qmake to include all the necessary header files in your Makefile as long as you double check that you have included all the necessary header files in your ".pro" file.

Finally, try making your virtual functions into pure virtual functions by setting them equal to zero. I'm not sure whether this will work but it's another good option that you can try.


11 comments:

  1. Thanks, I ran into to the same thing today... you saved me a bunch of time!

    ReplyDelete
  2. Hi! I'm glad this was useful, thanks for leaving a comment :)

    ReplyDelete
  3. After browsing lots of possible causes for this problem (missing destructors, member functions not implemented) this turned out to be my problem. Thanks! -Adam

    ReplyDelete
  4. Grr, unfortunately I didn't find this a few hours ago! Thanks for the solution though.

    ReplyDelete
  5. Yay! Another person this snippet almost helped :)

    ReplyDelete
  6. Thank you so much. I was about to give up and go to bed. Now its fixed I can sleep in peace :)

    ReplyDelete
  7. Hi Dagny,

    I'm glad you found the post helpful. Thanks for leaving a comment!

    ReplyDelete
  8. just say "run qmake" from Build menu & then "Rebuild All" again from Build menu....problem gets solved...atleast in my case it did.

    ReplyDelete
  9. This problem is also related with virtual functions. A common reason of the "vtable" message is not declaring a virtual function equal to zero, for example virtual foo(); instead of virtual foo() = 0;

    Hope it helps.

    ReplyDelete
  10. Your post was really helpful. I'm using Buildroot and though I added header to my *.pro I had also to delete build directory, so that pro file would be processed from very beginning. I think I will use CMake in my next project to avoid such situation, where I have to rerun project creation manually.

    ReplyDelete
  11. Thank You Thank You Thank You

    I ran into this problem a while back and figured it out too (after many hours). But I forgot all about it and today was plagued by it again. This time I put a BIG note in my project file in hopes that I will remember to look there the next time.

    ReplyDelete