Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

After adding Q_OBJECT , getting compile error



  • Hello, here is my header file of a very simple class.

    #ifndef MYTHREAD_H
    #define MYTHREAD_H

    #include <QObject>
    #include <QThread>

    class MyThread : public QThread
    {

    Q_OBJECT
    

    public:
    MyThread();
    void run();

    signals:
    void sending();
    };

    #endif // MYTHREAD_H

    I have corresponding CPP file. When I compile this project I am getting below error.

    g++ -m64 -o TestThread4 main.o mainwindow.o mythread.o moc_mainwindow.o -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread
    mythread.o: In function MyThread::MyThread()': /home/aminul/build-TestThread4-Qt_4_8_7_in_PATH_qt5-**Debug/../TestThread4/mythread.cpp:4: undefined reference tovtable for MyThread'
    Makefile:107: recipe for target 'TestThread4' failed**

    This file comiles fine, if I remove "Q_OBJECT" the project complies fine.

    What is the reason of this?


  • Lifetime Qt Champion

    @Aminul it's a linker error.

    You need to re-run qmake, then build again.

    If that doesn't help, last resort is to delete the build folder and then build again.



  • You have build the project before you add the Q_OBJECT. You can try to make some changes in the *.pro file such as add a space at the bottom. Then re-build it, usually it will solve this problem.


Log in to reply