56

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"));
}
ymoreau
  • 3,402
  • 1
  • 22
  • 60
TheFuzz
  • 2,607
  • 6
  • 29
  • 48

21 Answers21

52

When using Qt Creator:

  1. Build → Run qmake
  2. Build → Rebuild All
Honest Abe
  • 8,430
  • 4
  • 49
  • 64
Anurag Verma
  • 529
  • 4
  • 2
  • 3
    This answer is misleading. It makes perfect sense if the OP is using Qt Creator, but the OP only states Code::Blocks. To my knowledge, Code::Blocks doesn't come with a *run qmake* option on the build menu, and why would it? I can believe a plugin would add an option like that, but the OP doesn't mention use of any third party plugin. It is likely the case that the OP must still run qmake but telling them to use a menu option which probably doesn't exist is not going to be helpful. – Samuel Harmer Jan 23 '12 at 12:55
  • Correct, the solution (when not using Creator) is otherwise just running `qmake` from a command prompt. – peppe Jun 16 '16 at 18:40
  • Run qmake and then make for the exact effect - running make only generates the make file. Rather a misleading name, actually. – nerdguy Apr 30 '21 at 13:45
47

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.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
blwy10
  • 4,862
  • 2
  • 24
  • 23
  • 3
    I have the exact same problem as the OP, but in my case rerunning qmake does not solve it. This is only a partial solution that will work for only for some. – DarenW Jan 14 '11 at 19:44
  • 1
    DarenW: Check that the header with the class declaration is listed in HEADERS. – Frank Osterfeld Mar 29 '11 at 14:48
  • 1
    DarenW : thanks,it helps. i was thinking about this problem 3 hours. what is it? i didn't write manually qmake. is it somehow hole in qt, or what? why doesn't it compile, when we 've written all right? – Tebe Sep 18 '11 at 00:09
  • 6
    ***Do not run this***. This will wipe out your `.pro` file and any modification within. Running `qmake` alone (from the command line or from the Build menu in Creator) is sufficient. – peppe Jun 16 '16 at 17:01
  • 3
    **This answer is wrong and dangerous. That's all. You never have to do this unless you're starting from scratch and don't have an existing .pro file!** – Kuba hasn't forgotten Monica Oct 25 '16 at 21:26
16

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.

Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • Windows filesystems are not case-sensitive. – CMircea Mar 29 '11 at 12:54
  • 13
    You should still try to pay attention to case even on Windows... It's really annoying on Unix to check out a project and having to manually fix case issues like this because the original developer on Windows didn't care – Etienne Perot Mar 16 '12 at 02:24
10

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
Jeremy Friesner
  • 70,199
  • 15
  • 131
  • 234
  • Actual answer to this question, without platform/ide specific discussions or causes,clear and usable. Thank you. – Croll Jan 13 '16 at 13:28
  • With QTCreator you use the "Headers" section of the project and add your file there (it adds it to the HEADERS variable in the .pro) – Paul Hutchinson Mar 31 '16 at 19:43
  • QtCreator may add your header file name on the second line of the HEADERS variable in your .pro file. The first line then would be "HEADERS += \". This won't work. Erase the first empty entry and make it (in the OP case) : "HEADERS += AddressBook.h" – SR_ Oct 18 '17 at 15:04
6

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)
Dumisani Kunene
  • 659
  • 7
  • 9
4

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.

Community
  • 1
  • 1
  • I had the same problem with Qt Creator 1.3 under Ubuntu, and this worked for me. I don't know what's the underlying problem, though. – Martin Sep 13 '11 at 18:57
4

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

Felipe C.
  • 171
  • 1
  • 5
2

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.

yan bellavance
  • 4,710
  • 20
  • 62
  • 93
2

deleted the build folder, restarted Qt Creator and it worked

yolo
  • 2,757
  • 6
  • 36
  • 65
  • This may *well* have worked for you but, given the OP is not *using* Qt Creator, it's not really relevant. – paxdiablo Aug 01 '16 at 05:07
1

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.

user281754
  • 11
  • 1
1

I had the same problem trying to use a protected virtual function. Two things worked.

  1. Changing void process(); to void process() = 0;
  2. Making process() public instead of private
khafen
  • 151
  • 5
1

In my case Rebuild All was not enough, I had to delete build directory and make Rebuild All - then it worked!

Aleksei Petrenko
  • 6,698
  • 10
  • 53
  • 87
1

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.

Sunit Gautam
  • 5,495
  • 2
  • 18
  • 31
1

Just clear the project and then rebuild it!

Hamed
  • 36
  • 5
1

One cause is when you declare a virtual functions in a class and you don't define their body.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
agus
  • 11
  • 1
0

Go to .pro file and make sure .h file has 'include' before it. HEADERS += include/file.h \ include/file2.h

Phil
  • 405
  • 3
  • 14
0

You will get the same error message if you accidentally add a destructor prototype. Add an empty destructor definition or remove the prototype.

Emil Fors
  • 110
  • 6
0

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.

0

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 :)

0

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

-1

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

christangrant
  • 9,276
  • 4
  • 26
  • 27
  • I think that worked for you simply by making the `.pro` file newer than the `Makefile`, causing a re-`qmake`. That regenerates the dependencies so that the `moc` output is linked in to the application. – Toby Speight Nov 07 '17 at 09:37