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. qt - undefined reference to `vtable for myObj' in qt console application - signals and slots
QtWS25 Last Chance

qt - undefined reference to `vtable for myObj' in qt console application - signals and slots

Scheduled Pinned Locked Moved Unsolved General and Desktop
qobjectmacroclassvtableqt 5.7
11 Posts 7 Posters 8.0k Views
  • 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.
  • C Offline
    C Offline
    CybeX
    wrote on 25 Jan 2017, 02:59 last edited by
    #1

    I need to capture emited signals from a QProcess for testing purposes.

    Since I am using a console application, I resolved to create a class in my main.cpp file called myObj using mainly this example:

    #include <QCoreApplication>
    #include <QLoggingCategory>
    #include <QTextStream>
    #include <QProcess>
    #include <QString>
    #include <QVariant>
    #include <QDebug>
    #include <QObject>
    
    class myObj : public QObject
    {
    
        Q_OBJECT
    
    public:
        myObj(QObject *parent = 0);
    //    virtual ~Communicate();
        ~myObj();
    
    public slots:
        void registerFinished(int signal);
        void registerAboutToClose();
        void registerChannelReadyRead(int signal);
        void registerReadChannelFinished();
        void registerReadyRead();
        void registerReadyReadStandardOutput();
        void registerStarted();
    };
    
    myObj::myObj(QObject *parent)
        : QObject(parent)             <--- LINE 72 Error
    {
    
    }
    
    //virtual myObj::~Communicate(){
    
    //}
    
    myObj::~myObj(){                  <--- LINE 81 Error
    
    }
    
    void myObj::registerFinished(int signal){
        qDebug() << "exit code = " << QString::number(signal);
    }
    void myObj::registerAboutToClose(){
        qDebug() << "aboutToClose";
    }
    void myObj::registerChannelReadyRead(int signal){
        qDebug() << "channelReadyRead = " << QString::number(signal);
    }
    void myObj::registerReadChannelFinished(){
        qDebug() << "readChannelFinished";
    }
    void myObj::registerReadyRead(){
        qDebug() << "exit code";
    }
    void myObj::registerReadyReadStandardOutput(){
        qDebug() << "exit code";
    }
    void myObj::registerStarted(){
        qDebug() << "started";
    }
    
    myObj *myO;
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        myO = new myObj();
    
        //....
    }
    

    Problem:

    main.cpp:72: error: undefined reference to `vtable for myObj'

    main.cpp:81: error: undefined reference to `vtable for myObj'

    I have looked at a number of SO pages e.g here and here and here and various others, yet had not found a solution

    I have tried/done:

    • added the Q_Object Macro
    • ran qmake
    • rebuilt
    • checked the #include

    .pro file

    QT += core
    QT -= gui
    
    CONFIG += c++11
    
    TARGET = serv_app
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES += main.cpp
    

    Any suggestions?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hskoglund
      wrote on 25 Jan 2017, 03:09 last edited by hskoglund
      #2

      Hi, the Q_OBJECT macro in your class declaration needs to be processed by the moc compiler. Easiest is to move your class declaration into your main.h.
      EDIT: forgot probably you don't have a main.h yet, if so, just create it and put the class declaration inside it. And don't forget to #include main.h in your main.cpp :-)

      1 Reply Last reply
      1
      • Y Offline
        Y Offline
        yuvaram
        wrote on 25 Jan 2017, 04:12 last edited by yuvaram
        #3

        Hi CybeX,
        I have experimented your program. All the syntax are perfect, there is a issue with CLASS name (myObj).
        with class name myObj i m getting linker error.
        But by changing the class name and building there is no error. So may be there is some dependency issue with class name myObj.
        change it to myObj1 and try.

        Yuvaram Aligeti
        Embedded Qt Developer
        : )

        D 1 Reply Last reply 25 Jan 2017, 05:55
        0
        • Y yuvaram
          25 Jan 2017, 04:12

          Hi CybeX,
          I have experimented your program. All the syntax are perfect, there is a issue with CLASS name (myObj).
          with class name myObj i m getting linker error.
          But by changing the class name and building there is no error. So may be there is some dependency issue with class name myObj.
          change it to myObj1 and try.

          D Offline
          D Offline
          Devopia53
          wrote on 25 Jan 2017, 05:55 last edited by Devopia53
          #4

          @yuvaram

          Hi.

          You can just add the following line in main.cpp:

          [...]
          #include "main.moc"
          
          int main(int argc, char *argv[])
          {
          [...]
          
          

          and then run qmake.

          1 Reply Last reply
          1
          • Y Offline
            Y Offline
            yuvaram
            wrote on 25 Jan 2017, 06:37 last edited by
            #5

            @Devopia53
            Thank for correcting.

            But after adding #include "main.moc", (run qmake is also done) following error while building.

            Error: dependent 'debug\main.moc' does not exist.

            Yuvaram Aligeti
            Embedded Qt Developer
            : )

            D 1 Reply Last reply 25 Jan 2017, 07:46
            0
            • Y yuvaram
              25 Jan 2017, 06:37

              @Devopia53
              Thank for correcting.

              But after adding #include "main.moc", (run qmake is also done) following error while building.

              Error: dependent 'debug\main.moc' does not exist.

              D Offline
              D Offline
              Devopia53
              wrote on 25 Jan 2017, 07:46 last edited by
              #6

              @yuvaram

              Really? :(

              I'm works fine.
              If so, you can delete all of the previously built directories and then save main.cpp and rebuild all. Also, check where the main.moc file is generated.

              Good luck!.

              1 Reply Last reply
              1
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 26 Jan 2017, 00:03 last edited by
                #7

                Hi,

                If you really want to use that technique, the moc include should be the last line of main.cpp file

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • Y Offline
                  Y Offline
                  yuvaram
                  wrote on 26 Jan 2017, 01:28 last edited by
                  #8

                  Hi ,
                  main.moc file is not generating.

                  Yuvaram Aligeti
                  Embedded Qt Developer
                  : )

                  mrjjM D 2 Replies Last reply 26 Jan 2017, 07:32
                  1
                  • Y yuvaram
                    26 Jan 2017, 01:28

                    Hi ,
                    main.moc file is not generating.

                    mrjjM Offline
                    mrjjM Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 26 Jan 2017, 07:32 last edited by mrjj
                    #9

                    @yuvaram
                    Hi
                    For less pain. just use a .h file. :)

                    Delete the build folder and rebuild all.

                    1 Reply Last reply
                    1
                    • Y yuvaram
                      26 Jan 2017, 01:28

                      Hi ,
                      main.moc file is not generating.

                      D Offline
                      D Offline
                      Devopia53
                      wrote on 26 Jan 2017, 07:45 last edited by
                      #10

                      @yuvaram

                      If so, compile it with Network Download Example, which is officially available from Qt.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Stonge357
                        Banned
                        wrote on 8 Jun 2018, 10:28 last edited by
                        #11
                        This post is deleted!
                        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