Undefined reference to vtable. Trying to compile a Qt project
C++QtQmakeC++ Problem Overview
I'm using Code::Blocks 8.02 and the mingw 5.1.6 compiler. I'm getting this error when I compile my Qt project:
> C:\Documents and Settings\The > Fuzz\Desktop\GUI\App_interface.cpp|33|undefined > reference to `vtable for AddressBook'
File AddressBook.h:
#ifndef ADDRESSBOOK_H
#define ADDRESSBOOK_H
#include <QWidget>
class QLabel;
class QLineEdit;
class QTextEdit;
class AddressBook : public QWidget
{
Q_OBJECT
public:
AddressBook(QWidget *parent = 0);
private:
QLineEdit *nameLine;
QTextEdit *addressText;
};
#endif
File AddressBook.cpp:
#include <QtGui>
#include "addressbook.h"
AddressBook::AddressBook(QWidget *parent)
: QWidget(parent)
{
QLabel *nameLabel = new QLabel(tr("Name:"));
nameLine = new QLineEdit;
QLabel *addressLabel = new QLabel(tr("Address:"));
addressText = new QTextEdit;
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(nameLabel, 0, 0);
mainLayout->addWidget(nameLine, 0, 1);
mainLayout->addWidget(addressLabel, 1, 0, Qt::AlignTop);
mainLayout->addWidget(addressText, 1, 1);
setLayout(mainLayout);
setWindowTitle(tr("Simple Address Book"));
}
C++ Solutions
Solution 1 - C++
When using Qt Creator:
- Build → Run qmake
- Build → Rebuild All
Solution 2 - C++
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.
Solution 3 - C++
The problem is almost certainly that you are not compiling or not linking in the generated moc_AddressBook.cpp file. (It should have been generated for you -- you are running Qt's moc
on your code before compiling, right?)
To answer a little more thoroughly, the Q_OBJECT
macro signals Qt's moc
tool to create an extra implementation file that contains the code necessary to support QObject
's meta-information system. If you had any signals or slots, it would do a few things for those as well.
An alternative solution might be to remove the Q_OBJECT
macro. You probably don't want to do this, but it would help the immediate problem, and it isn't strictly necessary with the code that you've presented.
Also, I would note that your line:
#include "addressbook.h"
Should probably be:
#include "AddressBook.h"
based on how you presented the filenames in the question.
Solution 4 - C++
Assuming you are using qmake to generate your Makefile, be sure that AddressBook.h is specified in your .pro file's HEADERS's variable, e.g.
HEADERS = AddressBook.h
Solution 5 - C++
For CMake projects, set CMAKE_AUTOMOC to ON, this fixed my problem.
#Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed.
set(CMAKE_AUTOMOC ON)
Solution 6 - C++
I got this while using pure virtual functions. For example,
virtual void process();
gave this error, while
virtual void process() = 0;
made it go away.
For anyone who's Googling this problem, check that all virtual functions are defined. (via " = 0" or a full definition in the source file) I'm using Netbeans with the MinGW compiler.
Solution 7 - C++
I was running into the same problem using CLion, and solved it by adding these lines to CMakeLists.txt
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)
See https://www.jetbrains.com/help/clion/qt-tutorial.html#qt-setup-in-clion
Solution 8 - C++
I had the same problem but as soon as I defined my constructor in the header file instead of the .cpp the error disappeared. Also the corresponding moc file was missing in the file system and in the Makefile section"compiler_moc_header_make_all" . I ran a qmake then finally everything built with succes. I went to check the Makefile and it was there now.
Solution 9 - C++
deleted the build folder, restarted Qt Creator and it worked
Solution 10 - C++
I come to the same problem, rebuild the project never update the Makefile, I remove the Makefile and rebuild , the the problem is gone. ps: run 'make' from command line may give you detail information than the IDE, and helpful to get the real problem.
Solution 11 - C++
One cause is when you declare a virtual functions in a class and you don't define their body.
Solution 12 - C++
In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!
Solution 13 - C++
Simply Run qmake
for your project. This can easily be done by right-clicking on the name of your project and clicking on Run qmake
.
Solution 14 - C++
Just clear the project and then rebuild it!
Solution 15 - C++
Go to .pro file and make sure .h file has 'include' before it.
HEADERS += include/file.h
include/file2.h
Solution 16 - C++
I had the same problem trying to use a protected virtual function. Two things worked.
- Changing
void process();
tovoid process() = 0;
- Making
process()
public instead of private
Solution 17 - C++
You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.
Solution 18 - C++
Header files for moc compilation should contained in the HEADERS += ... variable:
I have moved the header files in the Myproject.pro to the SOURCES += ... section, because I want to have mySource.h and mySource.cpp in the same tree element. But that is faulty for QT Creator. In result the error "Undefined reference to vtable" has occured. It seems to be: QT detects header for moc compilation only in the HEADERS +=... section (or variable). See also the correct explaination in the other stackoverflow answer Second anwer "I've seen a lot of ways to solve the problem, but no explanation for why it happens, so here goes.". In my mind this is an exactly explaination of the problem, which has help me to found and solve my problem.
Solution 19 - C++
CMake
when using CMake, interestingly enough if my .cpp and .h files are not in the same folder the moc, bu default, fails to generate the meta file :)
Solution 20 - C++
the most errors from using Q_OBJECT but not generate moc file
the moc file define
classname::metaObject
staticMetaObject
qt_metacall
Here is a example:
when source or header using Q_OBJECT
/opt/qt/bin/moc ./window.h -o ./moc_window.cpp
Makefile add moc_window.o in OBJS
Ex: OBJS=main.o window.o moc_window.o
try this source
https://www.mediafire.com/file/anjeah1jmm07mfe/qt_group_box.tar.gz
refer to http://fatalfeel.blogspot.com/2013/11/qt5-c-building.html
Solution 21 - C++
I am using Qt creator to compile and run my programs, I don't use Qt command prompt often. One thing I did to get rid of the annoying error "vtable something something" is by adding the following lines to .pro
file.
> TEMPLATE = app
> QT += core