c++qtqmakemoc

Qt: undefined reference to `vtable for


When I try to compile my program I get the error

obj/backgroundWorker.o: In function `BackgroundWorker':
.../backgroundWorker.cpp:6: undefined reference to `vtable for BackgroundWorker'
obj/backgroundWorker.o: In function `~BackgroundWorker':
.../backgroundWorker.cpp:14: undefined reference to `vtable for BackgroundWorker'

I already found numerous reasons for this error, but so far, I couldn't solve the problem in my code:

backgroundWorker.hpp

#include <QObject>
#include <QPushButton>

class BackgroundWorker : public QWidget{

    Q_OBJECT

    public:
        explicit BackgroundWorker();
        ~BackgroundWorker();

        private slots:
            void start();

    private:
        QPushButton* mStartButton;
};

backgroundWorker.cpp

#include <iostream>
#include "getInput.hpp"
#include "backgroundWorker.hpp"
#include "LIF_network.hpp"

BackgroundWorker::BackgroundWorker(){
    mStartButton = new QPushButton("Start",this);
    mStartButton->setGeometry(QRect(QPoint(100,100),QSize(200,50)));

    connect(mStartButton, SIGNAL(released()), this, SLOT(start()));
}

BackgroundWorker::~BackgroundWorker(){
    delete mStartButton;
}

void BackgroundWorker::start(){
    //stuff not related to qt
}

main.cpp

#include <QApplication>
#include "backgroundWorker.hpp"

int main(int argc, char *argv[]){
    QApplication a(argc, argv);
    BackgroundWorker bw;
    return a.exec();
}

** .pro file**

CONFIG += qt debug c++11
QT += widgets

QMAKE_CC = clang++
QMAKE_CXX = clang++
QMAKE_CXXFLAGS += -Wall -Werror -O3
Headers += image.hpp \
            getInput.hpp \
            LIF_network.hpp \
            backgroundWorker.hpp \

SOURCES += main.cpp \
            image.cpp \
            getInput.cpp \
            LIF_network.cpp \
            backgroundWorker.cpp \


OBJECTS_DIR = ./obj

Solution

  • 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.