Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. moc file not found
Qt 6.12 LTS is out! See what's new in the release blog

moc file not found

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 8.4k Views 2 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.
  • C ChrisW67

    @Christian-Ehrlicher Beat me to it.

    The qmake "magic" that generates a Makefile rule for moc when it sees this style of include requires that it matches on the base name of the source file concerned.

    rpieketR Offline
    rpieketR Offline
    rpieket
    wrote on last edited by
    #8

    The mismatch is due to my attempt to simplify before posting. This is my actual main file, named tests.cpp. And yes, both TestOneToOne and TestOneToMany have the Q_OBJECT macro.

    #include <QCoreApplication>
    #include <QTest>
    
    #include "TestOneToOne.h"
    #include "TestOneToMany.h"
    
    int main(int argc, char *argv[])
    {
        int status = 0;
        status |= QTest::qExec(new TestOneToOne, argc, argv);
        status |= QTest::qExec(new TestOneToMany, argc, argv);
    
        return status;
    }
    
    #include "TestOneToOne.moc"
    #include "TestOneToMany.moc"
    
    
    Christian EhrlicherC 1 Reply Last reply
    0
    • rpieketR rpieket

      The mismatch is due to my attempt to simplify before posting. This is my actual main file, named tests.cpp. And yes, both TestOneToOne and TestOneToMany have the Q_OBJECT macro.

      #include <QCoreApplication>
      #include <QTest>
      
      #include "TestOneToOne.h"
      #include "TestOneToMany.h"
      
      int main(int argc, char *argv[])
      {
          int status = 0;
          status |= QTest::qExec(new TestOneToOne, argc, argv);
          status |= QTest::qExec(new TestOneToMany, argc, argv);
      
          return status;
      }
      
      #include "TestOneToOne.moc"
      #include "TestOneToMany.moc"
      
      
      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #9

      There is no need for foo.moc files when it's in a header. Then qmake/cmake automatically creates and adds moc_foo.cpp files.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      rpieketR 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        There is no need for foo.moc files when it's in a header. Then qmake/cmake automatically creates and adds moc_foo.cpp files.

        rpieketR Offline
        rpieketR Offline
        rpieket
        wrote on last edited by
        #10

        @Christian-Ehrlicher Without the moc files, I get:

        ld: Undefined symbols:
          vtable for TestOneToOne, referenced from:
              _main in tests.o
          vtable for TestOneToMany, referenced from:
              _main in tests.o
        

        With the moc files, it was working until this afternoon. Somehow I broke something, but I don't know what.

        Christian EhrlicherC C 2 Replies Last reply
        0
        • rpieketR rpieket

          @Christian-Ehrlicher Without the moc files, I get:

          ld: Undefined symbols:
            vtable for TestOneToOne, referenced from:
                _main in tests.o
            vtable for TestOneToMany, referenced from:
                _main in tests.o
          

          With the moc files, it was working until this afternoon. Somehow I broke something, but I don't know what.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #11

          Please show a minmal, compileable example and move code into source files and not in headers. qmake will not generate a moc_foo.cpp when there is no corresponding source file.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • rpieketR rpieket

            @Christian-Ehrlicher Without the moc files, I get:

            ld: Undefined symbols:
              vtable for TestOneToOne, referenced from:
                  _main in tests.o
              vtable for TestOneToMany, referenced from:
                  _main in tests.o
            

            With the moc files, it was working until this afternoon. Somehow I broke something, but I don't know what.

            C Offline
            C Offline
            ChrisW67
            wrote on last edited by ChrisW67
            #12

            The headers need to be listed in the PRO file:

            HEADERS += TestOneToOne.h TestOneToMany.h
            

            and qmake re-run. The magic moc includes are not required.

            Edit: Maybe @Christian-Ehrlicher has a point: a one-to-one header-source relationship.

            Christian EhrlicherC 1 Reply Last reply
            1
            • C ChrisW67

              The headers need to be listed in the PRO file:

              HEADERS += TestOneToOne.h TestOneToMany.h
              

              and qmake re-run. The magic moc includes are not required.

              Edit: Maybe @Christian-Ehrlicher has a point: a one-to-one header-source relationship.

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #13

              @ChrisW67 said in moc file not found:

              Edit: Maybe @Christian-Ehrlicher has a point: a one-to-one header-source relationship

              I'm not completely sure since I don't use qmake since 15 years or so but how else should qmake know that it has to moc those files.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #14

                Hi,

                You can see in this thread a way to put together multiple tests.

                That said, it will break the output. You should really keep the one class per test file.

                @Christian-Ehrlicher the moc file include is standard when you define classes in the implementation file. It has some other benefits.

                This blog article gives a pretty good summary about them.

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

                rpieketR 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  You can see in this thread a way to put together multiple tests.

                  That said, it will break the output. You should really keep the one class per test file.

                  @Christian-Ehrlicher the moc file include is standard when you define classes in the implementation file. It has some other benefits.

                  This blog article gives a pretty good summary about them.

                  rpieketR Offline
                  rpieketR Offline
                  rpieket
                  wrote on last edited by rpieket
                  #15

                  Minimal code. First main.cpp

                  #include <QCoreApplication>
                  #include <QTest>
                  QTEST_MAIN(TestQString)
                  

                  And qteststring.h:

                  #pragma once
                  #include <QObject>
                  class TestQString: public QObject
                  {
                      Q_OBJECT
                  private slots:
                      void toUpper();
                  };
                  

                  And qteststring.cpp:

                  #include <QTest>
                  #include "testqstring.h"
                  void TestQString::toUpper()
                  {
                      QString str = "Hello";
                      QCOMPARE(str.toUpper(), QString("HELLO"));
                  }
                  #include "testqstring.moc"
                  

                  Finally the auto-generated .pro file:

                  QT = core
                  
                  CONFIG += c++17 cmdline
                  
                  # You can make your code fail to compile if it uses deprecated APIs.
                  # In order to do so, uncomment the following line.
                  #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                  
                  SOURCES += \
                          main.cpp \
                          testqstring.cpp
                  
                  # Default rules for deployment.
                  qnx: target.path = /tmp/$${TARGET}/bin
                  else: unix:!android: target.path = /opt/$${TARGET}/bin
                  !isEmpty(target.path): INSTALLS += target
                  
                  HEADERS += \
                      testqstring.h
                  

                  Resulting output:

                  make: *** No rule to make target `testqstring.moc', needed by `testqstring.o'.  Stop.
                  make: *** Waiting for unfinished jobs....
                  ../Test_with_qmake/main.cpp:2:10: fatal error: 'QTest' file not found
                  #include <QTest>
                           ^~~~~~~
                  1 error generated.
                  make: *** [main.o] Error 1
                  14:28:39: The process "/usr/bin/make" exited with code 2.
                  Error while building/deploying project Test_with_qmake (kit: Qt 6.6.1 for macOS)
                  When executing step "Make"
                  
                  

                  BTW I have never seen a moc_*.cpp, only a *.moc, and then only until yesterday afternoon, when it stopped working altogether.
                  Should I be using cmake? I have no preference. I'm using qmake only because it was the default.

                  Christian EhrlicherC C 2 Replies Last reply
                  0
                  • rpieketR rpieket

                    Minimal code. First main.cpp

                    #include <QCoreApplication>
                    #include <QTest>
                    QTEST_MAIN(TestQString)
                    

                    And qteststring.h:

                    #pragma once
                    #include <QObject>
                    class TestQString: public QObject
                    {
                        Q_OBJECT
                    private slots:
                        void toUpper();
                    };
                    

                    And qteststring.cpp:

                    #include <QTest>
                    #include "testqstring.h"
                    void TestQString::toUpper()
                    {
                        QString str = "Hello";
                        QCOMPARE(str.toUpper(), QString("HELLO"));
                    }
                    #include "testqstring.moc"
                    

                    Finally the auto-generated .pro file:

                    QT = core
                    
                    CONFIG += c++17 cmdline
                    
                    # You can make your code fail to compile if it uses deprecated APIs.
                    # In order to do so, uncomment the following line.
                    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                    
                    SOURCES += \
                            main.cpp \
                            testqstring.cpp
                    
                    # Default rules for deployment.
                    qnx: target.path = /tmp/$${TARGET}/bin
                    else: unix:!android: target.path = /opt/$${TARGET}/bin
                    !isEmpty(target.path): INSTALLS += target
                    
                    HEADERS += \
                        testqstring.h
                    

                    Resulting output:

                    make: *** No rule to make target `testqstring.moc', needed by `testqstring.o'.  Stop.
                    make: *** Waiting for unfinished jobs....
                    ../Test_with_qmake/main.cpp:2:10: fatal error: 'QTest' file not found
                    #include <QTest>
                             ^~~~~~~
                    1 error generated.
                    make: *** [main.o] Error 1
                    14:28:39: The process "/usr/bin/make" exited with code 2.
                    Error while building/deploying project Test_with_qmake (kit: Qt 6.6.1 for macOS)
                    When executing step "Make"
                    
                    

                    BTW I have never seen a moc_*.cpp, only a *.moc, and then only until yesterday afternoon, when it stopped working altogether.
                    Should I be using cmake? I have no preference. I'm using qmake only because it was the default.

                    Christian EhrlicherC Offline
                    Christian EhrlicherC Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on last edited by
                    #16

                    @rpieket said in moc file not found:

                    #include <QTest>

                    Then you should also add the correct qmake command as written in the documentation.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    0
                    • rpieketR rpieket

                      Minimal code. First main.cpp

                      #include <QCoreApplication>
                      #include <QTest>
                      QTEST_MAIN(TestQString)
                      

                      And qteststring.h:

                      #pragma once
                      #include <QObject>
                      class TestQString: public QObject
                      {
                          Q_OBJECT
                      private slots:
                          void toUpper();
                      };
                      

                      And qteststring.cpp:

                      #include <QTest>
                      #include "testqstring.h"
                      void TestQString::toUpper()
                      {
                          QString str = "Hello";
                          QCOMPARE(str.toUpper(), QString("HELLO"));
                      }
                      #include "testqstring.moc"
                      

                      Finally the auto-generated .pro file:

                      QT = core
                      
                      CONFIG += c++17 cmdline
                      
                      # You can make your code fail to compile if it uses deprecated APIs.
                      # In order to do so, uncomment the following line.
                      #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
                      
                      SOURCES += \
                              main.cpp \
                              testqstring.cpp
                      
                      # Default rules for deployment.
                      qnx: target.path = /tmp/$${TARGET}/bin
                      else: unix:!android: target.path = /opt/$${TARGET}/bin
                      !isEmpty(target.path): INSTALLS += target
                      
                      HEADERS += \
                          testqstring.h
                      

                      Resulting output:

                      make: *** No rule to make target `testqstring.moc', needed by `testqstring.o'.  Stop.
                      make: *** Waiting for unfinished jobs....
                      ../Test_with_qmake/main.cpp:2:10: fatal error: 'QTest' file not found
                      #include <QTest>
                               ^~~~~~~
                      1 error generated.
                      make: *** [main.o] Error 1
                      14:28:39: The process "/usr/bin/make" exited with code 2.
                      Error while building/deploying project Test_with_qmake (kit: Qt 6.6.1 for macOS)
                      When executing step "Make"
                      
                      

                      BTW I have never seen a moc_*.cpp, only a *.moc, and then only until yesterday afternoon, when it stopped working altogether.
                      Should I be using cmake? I have no preference. I'm using qmake only because it was the default.

                      C Offline
                      C Offline
                      ChrisW67
                      wrote on last edited by ChrisW67
                      #17

                      @rpieket
                      The file names in the PRO file do not match those in your post.

                      Simple fix so that qmake now finds files.

                      A Qt test is just another Qt program, following all the same rules. It is just that the Test Lib writes main() for you to allow it to provide a lot of plumbing for you behind the scenes.
                      If you have the test class declaration in a separate header, as in this case, then that header should be listed in the PRO file HEADERS variable and not in the source file as an #include "blah.moc".

                      No longer get: make: *** No rule to make target `testqstring.moc', needed by `testqstring.o'. Stop

                      The QT variable in the PRO file needs QT += testlib so that the test Lib elements are included at compile and link time.
                      Rerun qmake after changing the PRO file

                      No longer get: ./Test_with_qmake/main.cpp:2:10: fatal error: 'QTest' file not found

                      Remaining problems are to do with the construction of the sources and not qmake.
                      main.cpp needs to #include "qteststring.h" so that the QTEST_MAIN macro expansion, which references TestString, knows what the class looks like.

                      Test compiles

                      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
                      • Unsolved