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!
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.
qt - undefined reference to `vtable for myObj' in qt console application - signals and slots
You have two options:
- @eyllanesc solution works for sure.
- add the line
#include "main.moc"
before or after youmain()
function.
When you put the class into its own header file, qmake
will generate the proper moc
file.
But when you put the class into a .cpp
file, the moc
code is not generated unless you put the line I said before.
Update #1
In the Qt tutorial about writing a Unit Test we can find the following info:
Note that if both the declaration and the implementation of our test
class are in a .cpp file, we also need to include the generated moc
file to make Qt's introspection work.
So this is another example where we need to include the moc
file.
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.
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
.
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
Stopping an Infinite Loop in C++ When Key Is Pressed
How to Turn on Multi-Cpu/Core C++ Compiles in the Visual Studio Ide (2008)
C++ Is It Necessary to Delete Dynamically Allocated Objects at the End of the Main Scope
a Few Things About Division by Zero in C
Stateful Functors & Stl:Undefined Behaviour
C++ Arrays as Function Arguments
Why Is My Class Non Default-Constructible
Segmentation Fault Before Main() When Using Glut, and Std::String
Observer Design Pattern in C++
Running a Windows Program and Detect When It Ends with C++
Building Qt 4.5 with Visual C++ 2010
How Is Push_Back Implemented in Stl Vector
What Is a C++11 Extension [-Wc++11-Extensions]
In C++ How Is Function Overloading Typically Implemented
Does Insertion to Stl Map Invalidate Other Existing Iterator