Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Unresolved external errors related to metaObject when building simple console application
Forum Updated to NodeBB v4.3 + New Features

Unresolved external errors related to metaObject when building simple console application

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KoenT
    wrote on last edited by
    #1

    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;;
    

    }
    @

    1 Reply Last reply
    0
    • TheBadgerT Offline
      TheBadgerT Offline
      TheBadger
      wrote on last edited by
      #2

      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)


      Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KoenT
        wrote on last edited by
        #3

        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;;
        

        }@

        1 Reply Last reply
        0
        • K Offline
          K Offline
          KoenT
          wrote on last edited by
          #4

          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!

          1 Reply Last reply
          0
          • TheBadgerT Offline
            TheBadgerT Offline
            TheBadger
            wrote on last edited by
            #5

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


            Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved