Unresolved external errors related to metaObject when building simple console application



  • Hi,

    I'm considering to switch to Qt for a new project.
    As a first step, I created a simple console application using QtCreator, and adjusted the code in main.cpp to what is shown below (to put my functionality in that Task class).
    When I now build the program, it gives the following linker error:

    @
    main.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall Task::metaObject(void)const " (?metaObject@Task@@UBEPBUQMetaObject@@XZ)@

    Why is the application not building any longer now?
    And what are the metaObject error messages I'm seeing?

    Thanks in advance for sharing any insights/tips!

    Koen

    PS: This is with Qt 5.3.2 on Windows7 64-bit using QtCreator 3.2.1 (and MSVC 2013 Ultimate).

    @
    #include <QCoreApplication>
    #include <QtCore>

    class Task : public QObject
    {
    Q_OBJECT

    public:
    Task(QObject *parent = 0) : QObject(parent) {}

    public slots:
    void run()
    {
    // Do processing here

        emit finished();
    }
    

    signals:
    void finished();
    };

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    // Task parented to the application so that it
    // will be deleted by the application.
    Task *task = new Task(&a);
    
    // This will cause the application to exit when
    // the task signals finished.
    QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
    
    // This will run the task from the application event loop.
    QTimer::singleShot(0, task, SLOT(run()));
    
    
    return a.exec&#40;&#41;;
    

    }
    @



  • My guess would be that you need to add the class Task into a header file (*.h) file and then run qmake on it again.

    I am not sure how to tell MOC to process a cpp file to generate the meta object info (if it is possible)



  • OK, I now created a MyTask class in the usual split header/implementation way (see below).
    This solved the first errors, but I still have another unresolved error:
    @main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall MyTask::MyTask(class QObject *)" (??0MyTask@@QAE@PAVQObject@@@Z) referenced in function _main@
    Strange, as the constructor is definitely implemented, and the MyTask.cpp file is also included in the project.

    If anyone has an idea about what might be going here?

    Koen

    @#ifndef MYTASK_H
    #define MYTASK_H

    #include <QObject>

    class MyTask : public QObject
    {
    Q_OBJECT
    public:
    explicit MyTask(QObject *parent = 0);

    signals:
    void finished();

    public slots:
    void run()
    {
    // Do processing here...

        emit finished();
    }
    

    };

    #endif // MYTASK_H@

    and

    @#include "MyTask.h"

    MyTask::MyTask(QObject *parent) :
    QObject(parent)
    {
    // nothing yet...
    }
    @

    with the main:

    @#include <QCoreApplication>
    #include <QtCore>
    #include "MyTask.h"

    int main(int argc, char *argv[])
    {
    QCoreApplication a(argc, argv);

    // Task parented to the application so that it
    // will be deleted by the application.
    MyTask *task = new MyTask(&a);
    
    // This will cause the application to exit when
    // the task signals finished.
    QObject::connect(task, SIGNAL(finished()), &a, SLOT(quit()));
    
    // This will run the task from the application event loop.
    QTimer::singleShot(0, task, SLOT(run()));
    
    return a.exec&#40;&#41;;
    

    }@



  • Update:
    when I do this in VS2013 with the Qt add-in installed, there seems to be no problems to build and run this code.

    Oh well, I guess I'll do that then and leave QtCreator alone for the time being. Still, if someone has any idea what might cause this in QtCreator, I'd be happy to learn about it!



  • Not sure if this might help but it is worth a try:
    Run clean, then qmake and then Rebuild in Qt Creator...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.