Q_Object Throwing 'Undefined Reference to Vtable' Error

Q_OBJECT throwing 'undefined reference to vtable' error

It is because the unit generated by MOC isn't included in the linking process. Or maybe it isn't generated at all. The first thing I'd do is to put the class declaration in a separate header file, perhaps the build system isn't scanning implementation files.

Another possibility is that the class in question once didn't belong to Qt meta object system (that is, it had no Q_OBJECT or maybe didn't inherit from QObject at all), so qmake needs to be run again in order to create the necessary rules for MOC. The easiest way to force qmake to be run is to make some insignificant changes to the project file to update its timestamp, like adding and then removing some white space. Or, if you're using Qt Creator, then just select “Run qmake” from the project context menu.

Qt C++ Q_OBJECT error undefined reference to vtable

To use the QOBJECT macro in your class you need to extend QObject.

class MyObject: public QObject
{
Q_OBJECT

public:
MyObject (QObject *_parent);

.....

};

Undefined reference to vtable

So, I've figured out the issue and it was a combination of bad logic and not being totally familiar with the automake/autotools world. I was adding the correct files to my Makefile.am template, but I wasn't sure which step in our build process actually created the makefile itself. So, I was compiling with an old makefile that had no idea about my new files whatsoever.

Thanks for the responses and the link to the GCC FAQ. I will be sure to read that to avoid this problem occurring for a real reason.

QObject: Missing vtable link error

You need to include this line at the end of your source file:

#include "MainController.moc"

Alternatively, you can also handle this with your buildsystem, but that is probably the former is easier.

linker error: undefined reference to `vtable of QGLViewer’

You were not running glviewer.h through moc. That's what the error is saying. In your .pro file, change

HEADERS  += mainwindow.h \
glviewer.cpp

to

HEADERS  += mainwindow.h \
glviewer.h <------


Related Topics



Leave a reply



Submit