Qt undefined reference to vtable
This is a subtle bug (and probably partly at least a compiler bug) that I've seen before. Since QWidget has a virtual destructor, the compiler needs a vtable for your class. But your class doesn't have any virtual functions, so it didn't build one for your Communicate class.
Add a virtual ~Communicate() {};
to your class, and all will be well.
Yes, it took me some time to figure this out too!
How resolve undefined reference to vtable in c++ on QT
Typically, errors like that can be resolved by running QMake. Anytime you create a new class that derives from QObject
, Qt's model meta-object compiler (MOC) needs to auto-generate the code for the meta-class of the new class -- QMake ensures that this happens.
If you are using Qt Creator, just select Run qmake
from the Build
menu.
You may also have to run Clean project X
or Clean all
, also found in the Build
menu.
The MOC can be temperamental, so you need to do the following:
- Move your
QObject
-derived class into a separate source and header file (In your case, createbot.h
andbot.cpp
, and move the class code there) - Separate declaration and definition of your slot code (define
txt
asbot::txt
inbot.cpp
)
The MOC generates a corresponding meta-class file (moc_bot.cpp
, in your case), and sometimes gets confused when there are multiple QObject
-derived classes in one file. Best practice is to use one header and one source file for each QObject
-derived class.
If all else fails, you may need to delete the .pro.user
file for your project, and exit and restart Qt Creator. Then from Build
menu, choose Clean all
, Run qmake
, Rebuild All
.
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.
Undefined reference to vtable. Trying to compile a Qt project
Warning: Do not do this if you already have a .pro file - you'll lose it!
In order to automatically ensure that all moc cpp files are generated, you can get qmake to automatically generate a .pro file for you instead of writing one yourself.
Run
qmake -project
in the project directory, and qmake will scan your directory for all C++ headers and source files to generate moc cpp files for.
Nothing helped to solve Undefined reference to vtable in Qt
Your problem is caused by the fact that you have the declaration of a class subclassing QObject
and having Q_OBJECT
macro inside it within a .cpp
file; That macro contains, among other things, the declaration of a virtual method:
virtual const QMetaObject *metaObject() const;
qmake
tool only processes header files to locate QObject
subclasses containing Q_OBJECT
macro to automatically generate the actual code corresponding to the declarations produced by Q_OBJECT
macro. In your case, qmake
does not process main.cpp
file and hence it is left containing unimplemented virtual function - hence the linker error you are receiving.
The proper solution is to move the declaration of the class to a separate header file and add the header to your project. There's, however, another solution which is kinda hacky but it can be used if you for some reason absolutely need to leave the declaration of your class inside the .cpp
file:
class CustomUndoStack : public QObject
{
Q_OBJECT
public:
CustomUndoStack(QObject * parent = 0) : QObject(parent) {}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication qCoreApplication(argc, argv);
CustomUndoStack uStack;
return qCoreApplication.exec();
}
Notice the #include "main.moc"
line. The presence of this line forces qmake
to actually process the declarations from your .cpp
file and generate the required .moc
file which contains the autogenerated code corresponding to the declarations produced by Q_OBJECT
macro.
Qt: Undefined reference to 'vtable'
Absence of a vtable is usually a symptom of failing to include the output of moc
in the linker arguments. Make sure that you ran moc
on your header, and that you linked the result.
Note that if you're using qmake
, you may need to re-run qmake
to generate new makefiles if you change a class that was not Q_OBJECT
so that is now Q_OBJECT
- it will not otherwise know that moc
should be run.
In passing, it's a good idea to add a constructor that takes an optional parent QObject
, to get some of the benefits of Qt's memory management (freeing of child objects) where the user wants it.
Related Topics
C++: Life Span of Temporary Arguments
How to Make Cin Take Only Numbers
Enum to String in Modern C++11/C++14/C++17 and Future C++20
Is Std::Unique_Ptr≪T≫ Required to Know the Full Definition of T
Finding C++ Static Initialization Order Problems
Difference Between Char A[] = String; and Char *P = String;
Why Is There No Call to the Constructor
How to Use Boost in Visual Studio 2010
Is There a Simple Way to Convert C++ Enum to String
Why Isn't It Legal to Convert "Pointer to Pointer to Non-Const" to a "Pointer to Pointer to Const"
What Are Access Specifiers? Should I Inherit With Private, Protected or Public
Why Are Elementwise Additions Much Faster in Separate Loops Than in a Combined Loop
How to Serialize an Object in C++
What's the Rationale For Null Terminated Strings