开发者

qt error: undefined reference to `vtable for Thread'

开发者 https://www.devze.com 2023-03-13 03:48 出处:网络
I have the code: #include <iostream> #include <QThread> #include <unistd.h> #include <stdlib.h>

I have the code:

#include <iostream>
#include <QThread>
#include <unistd.h>
#include <stdlib.h>
#include <QApplication>


using std::cerr;
using std::endl;

class Thread : public QThread
{
    Q_OBJECT
public:
    Thread();
    ~Thread();
    void setMessage(const QString &_message);
    void stop();
protected:
    void run();
private:
    QString message;
    volatile bool stopped;
};

Thread::Thread()
{
    stopped = false;    
    run();
}

Thread::~Thread()
{

}


void Thread::run()
{
    while(!stopped){
        cerr << qPrintable(message);
        sleep(1);
    }
        stopped = false;
        cerr << endl;
}

void Thread::stop()
{
    stopped = true; 
}

void Thread::setMessage(const QString &_message)
{
    message = _message; 
}

int main(int argc,char *argv[])
{
    QApplication app(argc, argv);
    Thread *A,*B;
    A = new Thread();
    B = new Thread();
    A->setMessage("Thread A\n");
    B->setMessage("Thread B\n");
//.run();
//.run();
    sleep(10);
    A->stop();
    B->stop();
    return 0;
}

and i have error

g++ -Wl,--hash-style=gnu -Wl,--as-needed -Wl,-O1 -o tmp main.o    -L/usr/lib -lQtGui -lQtCore -lpthread 
main.o: In fun开发者_如何学Goction `Thread::~Thread()':
main.cpp:(.text+0xa): undefined reference to `vtable for Thread'
main.o: In function `Thread::Thread()':
main.cpp:(.text+0x1da): undefined reference to `vtable for Thread'
collect2: ld returned 1 exit status
make: *** [tmp] Error 1


You must generate a header with moc. This can be done automatically with the Qt build system. Instead of using gcc directly, you should use a qmake file.

Also you should probably separate the declaration and code into header file and cpp file.

Here is a description of what moc does: http://doc.qt.nokia.com/latest/moc.html

And a similar question (with answers) here on stackoverflow: Undefined reference to vtable. Trying to compile a Qt project


You need to have a line at the bottom of your source file:

#include "main.moc"

That's because the declaration of class Thread isn't in a header - it's in a .cpp file. So by default moc won't run on it. Adding the line does two things:

  • it signals to qmake and moc that moc has to process the .cpp file
  • it causes the stuff that moc generates to be pulled in by the compile step

So after adding that line you'll need to rerun qmake so it can update the makefiles to cause main.moc to be generated.

Normally, moc runs against header files and creates .cpp files that get included in the build (qmake sees to this). This 'trick' causes moc to also be run on the .cpp filein question (and to have the moc generated code compiled in).

An alternative to including main.moc at the end of main.cpp is to move the definition of class Thread to a .h header file and #include that. If the definition is in a header qmake and moc should handle things automatically.


I think that the qmake system requires that your header file include directly. Mine was failing to generate a moc until I added that include.


Simple add string to .pro file. This was helping for me in same problem

INSTALLS += target

See queudcustomtype example.

0

精彩评论

暂无评论...
验证码 换一张
取 消