November 9, 2011

moorepe moorepe
Lab Rat
4 posts

[SOLVED] getting repeated problem undefined reference to vtable with the QtCreator

 

Every now and then i get a problem which i suspect is a problem in QtCreator because it eventually compiles properly after touching the project file. I get undefined reference to vtable for a newly created class constructor (code and pro file is below). After getting the error i simplified the code to very basic form (seen below) and still the problem remained even after repeated Clean project and rebuild steps.

Then i turned my attention to the pro file where I suspected that the HEADERS line had some weird character in it and changed the line from …

  1. HEADERS += \
  2. atimer.h

to ….
  1. HEADERS += atimer.h

it then worked. Then i did a ctrl Z to back out the changes but could not reproduce the error.

Has anyone else seen this behavior?

Seen on XP and Win 7.

  1. #ifndef ATIMER_H
  2. #define ATIMER_H
  3. #include <QObject>
  4. class ATimer : public QObject
  5. {
  6.     Q_OBJECT
  7. public:
  8.     ATimer();
  9. };
  10. #endif // ATIMER_H
  11.  
  12.  
  13. #include "atimer.h"
  14. ATimer::ATimer()
  15. {
  16. }

  1. QT       += core
  2.  
  3. QT       -= gui
  4.  
  5. TARGET = TimerTest
  6. CONFIG   += console
  7. CONFIG   -= app_bundle
  8.  
  9. TEMPLATE = app
  10.  
  11.  
  12. SOURCES += main.cpp \
  13.     atimer.cpp
  14.  
  15. HEADERS += \
  16.     atimer.h

Thanks for replies. It is indeed solved by running qmake. When I touched the project file it implicitly runs qmake.

For anyone interested, the reason this happens is when you add the Q_OBJECT macro (the magic Qt compiler thing) to a class that was previously build without it.

When you add Q_OBJECT to your class, the moc.exe (the meta object compiler) needs to run on your source to generate a moc_<classname>.cpp for proper compilation.

So after rerunning QMake your project will fill in these additional targets in the makefiles.

  1. compiler_moc_header_make_all: debug/moc_atimer.cpp
  2. compiler_moc_header_clean:
  3.  -$(DEL_FILE) debug\moc_atimer.cpp
  4. debug/moc_atimer.cpp: ../../LearnQt/TimerTest/atimer.h
  5.  C:\Qt\2010.04\qt\bin\moc.exe $(DEFINES) $(INCPATH) -D__GNUC__ -DWIN32 ..\..\LearnQt\TimerTest\atimer.h -o debug\moc_atimer.cpp

6 replies

November 9, 2011

fluca1978 fluca1978
Ant Farmer
524 posts

Never seen on Linux, I don’t develop under Windows so I cannot test and confirm. Should not this thread belongs to the tools section?

November 9, 2011

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Re-running qmake should solve to problem.

November 9, 2011

moorepe moorepe
Lab Rat
4 posts

When you rebuild is that not rerunning qmake ?

November 9, 2011

Hostel Hostel
Hobby Entomologist
185 posts

Sometimes this error is when we add a new file which is QObject type and we doesn’t run qmake. I’m not sure in 100% but I think that full build forces a qmake.

November 9, 2011

mkoskim mkoskim
Lab Rat
31 posts

Yes, I have noticed that at least Qt Creator version that comes with Ubuntu has some problems handling dependencies for generated files. Re-running qmake and rebuilding the project usually helps. You might of course get the same error message, if you have no implementation to a virtual function; check that your header and implementation matches, if the problem does not go away with rebuilding.

November 10, 2011

Asperamanca Asperamanca
Hobby Entomologist
371 posts

I get these errors every couple of days. My standard procedure (until it works) is:
-) Run qmake
-) Manually delete the obj files of the source modules I know need to be re-compiled, maybe also delete certain Makefiles (if I am in a multi-sub-project environment, and only want to rebuild one of those projects)
-) Do a clean, manually delete any Makefiles, and rebuild

Usually, deleting the corresponding obj file is enough.

Edit: BTW, Windows XP here

 
  ‹‹ [SOLVED] How to set an item in a QTreeView as selected?      [Solved] Performance QLabel ››

You must log in to post a reply. Not a member yet? Register here!