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
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 when compiling a Qt hello World
this is often an dependency issue where the moc-compiler where not invoked due to an already existing object file. try to distclean your environment and start qmake again. hope that helps
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.
Qt: undefined reference to `vtable for
As Frank Osterfeld mentioned in the comment the proper name for the variable is upper case HEADERS
. And it should contain the list of all headers that have Q_OBJECT
macro in them
This is required because qmake needs to know the list of header files to feed to the moc
.
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
.
Related Topics
What Is the Performance Penalty of C++11 Thread_Local Variables in Gcc 4.8
System("Command") Produces an Error; But It Works When Invoked Directly from Bash Prompt
Problems Cross Compiling Qt 4.7 from Source for Arm
How to Multicast with Ipv6 Udp Socket in C/C++ on Linux
Why Is Padding Added for Multiple Data Members of Structures and Not for Single Members
Wait Until User Presses Enter in C++
Single Process Maximum Possible Memory in X64 Linux
Differencebetween Crtbegin.O, Crtbegint.O and Crtbegins.O
Send Mail Using Smtp in C++ on Linux
(C/C++) How to Generate Executable File That Can Run on Both Windows and Linux
How to Compile Curlpp on Ubuntu
How Can My C/C++ Application Determine If the Root User Is Executing the Command
Boost Spirit Semantic Action Parameters