nonForgivingJesus
nonForgivingJesus

Reputation: 593

Qt Quick + CMake + custom QObject results in undefined reference to `vtable'

I use Qt Quick to make small application to work with files. Everything was working until one moment i executed in my build/ folder

rm -rf * 
cmake ..
make

And then make halted with this error (lising is huge, i suppresed non-important part):

[100%] Linking CXX executable uint32_sort_gui

In function `LibController::~LibController()':
lib_controller.cpp:(.text+0x10): undefined reference to `vtable for LibController'
main.cpp.o: In function `int qmlRegisterType<LibController>(char const*, int, int, char const*)':

...

Here is my .hpp and .cpp files of the class:

lib_controller.hpp

#include <QObject>
#include <QString>

class LibController : public QObject{
    Q_OBJECT
    Q_PROPERTY(decltype(getProgress) progress READ getProgress NOTIFY ProgressChanged)
    public:
        explicit LibController(QObject *parent = 0);
        ~LibController(); 

        double getProgress();

        Q_INVOKABLE
        void addFile(QString from_name, QString to_name);
        Q_INVOKABLE
        void sortFile();
    signals:
        void ProgressChanged();

    private:
        double current_progress;
        FileManager* file_manager;
};

lib_controller.cpp

#include "lib_controller.hpp"

LibController::~LibController(){
    delete file_manager;
}

double LibController::getProgress(){...}

void LibController::addFile(QString from_name, QString to_name){...}

void LibController::sortFile(){...}

main.cpp

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlComponent>

#include "lib_controller.hpp"

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);

    // Registration of custom type
    qmlRegisterType<LibController>("com.sort.controller", 0, 1, "LibController");

    ...

    return app.exec();
}

And my CMakeLists.txt configuration.

I read another questions about this problem, but cleaning and rebuilding did not help (i even accidentally deleted whole project folder).

The problem persists, and i do not understand, how to fix it ...

UPD:

Here is full error message

UPD2:

After excluded LibController from the project and recompiling it, error is gone, but no window is showed to me. I can see from terminal that it is running, but no GUI popped up.

I guess problem is not in LibController, but somewhere else.

Upvotes: 0

Views: 953

Answers (3)

Teivaz
Teivaz

Reputation: 5675

You need to add headers to the list of your source files for cmake to be able to run AUTOMOC on them.

This question has been already asked and answered here Changing location of header file causes missing vtable error when compiling with CMake

Upvotes: 1

nonForgivingJesus
nonForgivingJesus

Reputation: 593

According to comments in this question, you should

  1. Rename your custom object header file to moc_*your header name*.hpp
  2. Add path to this header to add_executable() function
  3. Clear build/ directory and rerun CMake
  4. Be somewhat happy in the end of the day

Hope this will help somebody, who tried to find last resort, like me.

Upvotes: 0

Mike Z
Mike Z

Reputation: 21

Since you call out the constructor in your header try adding this to the cpp file:

LibController::LibController(QObject *parent) : QObject(parent) {
}

Also, I have found that Rebuild All doesn't re-run qmake all the time. If you still see the issue run Build, Run qmake to make sure the header is getting processed.

Upvotes: 0

Related Questions