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. Moc not generating functions

Moc not generating functions

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 3 Posters 1.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.
  • R Offline
    R Offline
    Redman
    wrote on last edited by Redman
    #1

    I have following class
    .h

    #define CONFIGURATION_H
    
    #include <QObject>
    #include <QtCore>
    
    class Configuration : public QObject {
       Q_OBJECT
    
     public:
       explicit Configuration(QObject* parent = nullptr);
       ~Configuration();
    
       void init();
    
     protected:
     private:
     public slots:
     private slots:
    };
    
    #endif CONFIGURATION_H
    

    .cpp

    #include "Configuration.h"
    
    Configuration::Configuration(QObject* parent) : QObject(parent) {}
    
    Configuration::~Configuration() {}
    
    void Configuration::init() {}
    
    #include "moc_Configuration.cpp"
    

    When using Configuration from main.cpp it's working.
    When using Configuration from the TestSuit (Qt Test) I get:

    • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
    • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: virtual __cdecl Configuration::~Configuration(void)" (??1Configuration@@UEAA@XZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
    • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: void __cdecl Configuration::init(void)" (?init@Configuration@@QEAAXXZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".

    I'm using the class as followed in main

    #include <QCoreApplication>
    #include <QtCore>
    
    #include "Configuration/Configuration.h"
    
    int main(int argc, char* argv[]) {
       // Instancing the application
       QCoreApplication a(argc, argv);
    
       Configuration config;
       config.init();
    
       // Getting the result of the application
       int result(a.exec());
    
       // Returning the result
       return result;
    }
    

    in the TestSuit as followed:

    #include <QObject>
    #include <QTest>
    #include <QtCore>
    
    #include "../../../app/src/Configuration/Configuration.h"
    
    class TestConfig : public QObject {
       Q_OBJECT
    
     public:
     private slots:
       void initTestCase();
       void init();
       void cleanup();
       void cleanupTestCase();
    
       // Allows access to private members without accessors
       friend class Configuration;
    };
    
    void TestConfig::initTestCase() {
       Configuration cfg;
    }
    
    void TestConfig::init() {}
    
    void TestConfig::cleanup() {}
    
    void TestConfig::cleanupTestCase() {}
    
    QTEST_APPLESS_MAIN(TestConfig)
    
    #include "tst_Configuration.moc"
    
    

    I only have a vague idea of what is happening. Can anyone enlighten me on why it's not working and how to fix this problem?

    Christian EhrlicherC 1 Reply Last reply
    0
    • R Offline
      R Offline
      Redman
      wrote on last edited by
      #18

      Solution was to create SUBDIR .pro with lib and test subdirs. In lib I build a .dll from app and include the dll in test
      1c411b43-060b-429b-9589-0f8263271149-image.png

      1 Reply Last reply
      0
      • R Redman

        I have following class
        .h

        #define CONFIGURATION_H
        
        #include <QObject>
        #include <QtCore>
        
        class Configuration : public QObject {
           Q_OBJECT
        
         public:
           explicit Configuration(QObject* parent = nullptr);
           ~Configuration();
        
           void init();
        
         protected:
         private:
         public slots:
         private slots:
        };
        
        #endif CONFIGURATION_H
        

        .cpp

        #include "Configuration.h"
        
        Configuration::Configuration(QObject* parent) : QObject(parent) {}
        
        Configuration::~Configuration() {}
        
        void Configuration::init() {}
        
        #include "moc_Configuration.cpp"
        

        When using Configuration from main.cpp it's working.
        When using Configuration from the TestSuit (Qt Test) I get:

        • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
        • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: virtual __cdecl Configuration::~Configuration(void)" (??1Configuration@@UEAA@XZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".
        • C:\Dev\Build\ProjectPewPew\Release\6.5.2\x64\src\service\test\tst_Configuration.obj:-1: error: LNK2019: Verweis auf nicht aufgel”stes externes Symbol ""public: void __cdecl Configuration::init(void)" (?init@Configuration@@QEAAXXZ)" in Funktion ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".

        I'm using the class as followed in main

        #include <QCoreApplication>
        #include <QtCore>
        
        #include "Configuration/Configuration.h"
        
        int main(int argc, char* argv[]) {
           // Instancing the application
           QCoreApplication a(argc, argv);
        
           Configuration config;
           config.init();
        
           // Getting the result of the application
           int result(a.exec());
        
           // Returning the result
           return result;
        }
        

        in the TestSuit as followed:

        #include <QObject>
        #include <QTest>
        #include <QtCore>
        
        #include "../../../app/src/Configuration/Configuration.h"
        
        class TestConfig : public QObject {
           Q_OBJECT
        
         public:
         private slots:
           void initTestCase();
           void init();
           void cleanup();
           void cleanupTestCase();
        
           // Allows access to private members without accessors
           friend class Configuration;
        };
        
        void TestConfig::initTestCase() {
           Configuration cfg;
        }
        
        void TestConfig::init() {}
        
        void TestConfig::cleanup() {}
        
        void TestConfig::cleanupTestCase() {}
        
        QTEST_APPLESS_MAIN(TestConfig)
        
        #include "tst_Configuration.moc"
        
        

        I only have a vague idea of what is happening. Can anyone enlighten me on why it's not working and how to fix this problem?

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

        @Redman said in Moc not generating functions:

        #define CONFIGURATION_H

        don't you miss a '#ifndef CONFIGURATION_H' here?

        Please show your CMakeLists.txt - I would guess you don't link against the lib where configuration.cpp is compiled with.

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

        R 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          @Redman said in Moc not generating functions:

          #define CONFIGURATION_H

          don't you miss a '#ifndef CONFIGURATION_H' here?

          Please show your CMakeLists.txt - I would guess you don't link against the lib where configuration.cpp is compiled with.

          R Offline
          R Offline
          Redman
          wrote on last edited by
          #3

          @Christian-Ehrlicher
          #ifndef CONFIGURATION_H is present. I missed it while copying.

          My project structure:
          457dd8ba-0012-4297-8a30-71d318db39b5-image.png

          test.pro

          QT += testlib
          QT -= gui
          
          TARGET = TestService
          
          CONFIG += qt console warn_on depend_includepath testcase
          CONFIG -= app_bundle
          CONFIG -= debug_and_release
          
          TEMPLATE = app
          
          SOURCES += \
              $$files("$$PWD/src/*.cpp", true)
          
          Christian EhrlicherC S 2 Replies Last reply
          0
          • R Redman

            @Christian-Ehrlicher
            #ifndef CONFIGURATION_H is present. I missed it while copying.

            My project structure:
            457dd8ba-0012-4297-8a30-71d318db39b5-image.png

            test.pro

            QT += testlib
            QT -= gui
            
            TARGET = TestService
            
            CONFIG += qt console warn_on depend_includepath testcase
            CONFIG -= app_bundle
            CONFIG -= debug_and_release
            
            TEMPLATE = app
            
            SOURCES += \
                $$files("$$PWD/src/*.cpp", true)
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #4

            You don't compile configuration.cpp anywhere in your pro-file so ... or is it in testcase?

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

            R 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              You don't compile configuration.cpp anywhere in your pro-file so ... or is it in testcase?

              R Offline
              R Offline
              Redman
              wrote on last edited by Redman
              #5

              @Christian-Ehrlicher
              Configuration.cpp is in service->app->src->Configuration
              Its compiled correct and can be used from the main.cpp which is in service->app->src

              app.pro

              QT += \
                  core
              
              TARGET = service
              TEMPLATE = app
              
              CONFIG += c++17
              # Stop make from creating these folders in the build folder
              CONFIG -= debug_and_release
              
              MOC_DIR = $$OUT_PWD/tmp
              OBJECTS_DIR = $$OUT_PWD/tmp
              UI_DIR = $$OUT_PWD/tmp
              RCC_DIR = $$OUT_PWD/tmp
              
              SOURCES += \
                  $$files("$$PWD/*.cpp",true) \
              
              HEADERS += \
                  $$files("$$PWD/src/*.h",true)
              
              Christian EhrlicherC 1 Reply Last reply
              0
              • R Redman

                @Christian-Ehrlicher
                Configuration.cpp is in service->app->src->Configuration
                Its compiled correct and can be used from the main.cpp which is in service->app->src

                app.pro

                QT += \
                    core
                
                TARGET = service
                TEMPLATE = app
                
                CONFIG += c++17
                # Stop make from creating these folders in the build folder
                CONFIG -= debug_and_release
                
                MOC_DIR = $$OUT_PWD/tmp
                OBJECTS_DIR = $$OUT_PWD/tmp
                UI_DIR = $$OUT_PWD/tmp
                RCC_DIR = $$OUT_PWD/tmp
                
                SOURCES += \
                    $$files("$$PWD/*.cpp",true) \
                
                HEADERS += \
                    $$files("$$PWD/src/*.h",true)
                
                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @Redman said in Moc not generating functions:

                SOURCES +=
                $$files("$$PWD/*.cpp",true) \

                Apart from the really strange way to add sources - this does not add configuration.cpp.

                As you can see in the linker error not the moc stuff is missing but the functions defined in configuration.cpp

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

                R 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Redman said in Moc not generating functions:

                  SOURCES +=
                  $$files("$$PWD/*.cpp",true) \

                  Apart from the really strange way to add sources - this does not add configuration.cpp.

                  As you can see in the linker error not the moc stuff is missing but the functions defined in configuration.cpp

                  R Offline
                  R Offline
                  Redman
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher
                  Any tips on how to add sources without typing every file specifically. I assumed as long as the files showed up in the project tree building them is no problem. Which was true until now.

                  Christian EhrlicherC 1 Reply Last reply
                  0
                  • R Redman

                    @Christian-Ehrlicher
                    Any tips on how to add sources without typing every file specifically. I assumed as long as the files showed up in the project tree building them is no problem. Which was true until now.

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

                    @Redman Type all. But that's not the problem here.

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

                    R 1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @Redman Type all. But that's not the problem here.

                      R Offline
                      R Offline
                      Redman
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher So how can I tell moc to add the functions?

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • R Redman

                        @Christian-Ehrlicher So how can I tell moc to add the functions?

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

                        @Redman said in Moc not generating functions:

                        @Christian-Ehrlicher So how can I tell moc to add the functions?

                        As I already said there is no problem with moc but you don't compile configuration.cpp anywhere.

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

                        R 1 Reply Last reply
                        0
                        • Christian EhrlicherC Christian Ehrlicher

                          @Redman said in Moc not generating functions:

                          @Christian-Ehrlicher So how can I tell moc to add the functions?

                          As I already said there is no problem with moc but you don't compile configuration.cpp anywhere.

                          R Offline
                          R Offline
                          Redman
                          wrote on last edited by
                          #11

                          @Christian-Ehrlicher
                          The service.exe, which is configured in app.pro, is built correct. The exe can only be a result of a successful compilation. So I'd assume configuration.cpp is compiled

                          Christian EhrlicherC 1 Reply Last reply
                          0
                          • R Redman

                            @Christian-Ehrlicher
                            The service.exe, which is configured in app.pro, is built correct. The exe can only be a result of a successful compilation. So I'd assume configuration.cpp is compiled

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

                            @Redman said in Moc not generating functions:

                            So I'd assume configuration.cpp is compiled

                            Don't assume, check.
                            You did not show any pro file where you compile configuration.cpp so my answer still stands...

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

                            R 1 Reply Last reply
                            0
                            • Christian EhrlicherC Christian Ehrlicher

                              @Redman said in Moc not generating functions:

                              So I'd assume configuration.cpp is compiled

                              Don't assume, check.
                              You did not show any pro file where you compile configuration.cpp so my answer still stands...

                              R Offline
                              R Offline
                              Redman
                              wrote on last edited by Redman
                              #13

                              @Christian-Ehrlicher
                              758ed0ad-61b4-482a-a62c-d0270c00b542-image.png

                              I think this should answer your question.

                              To elaborate further:
                              project.pro

                              
                                SUBDIRS = \
                                          client \
                                          logger \
                                          service
                              			
                                # where to find the sub projects - give the folders
                                client.subdir = src/client
                                logger.subdir  = src/logger
                                service.subdir  = src/service
                              

                              service.pro

                              TEMPLATE = subdirs
                              
                              SUBDIRS = \
                                      app \
                                      test \
                              
                              # where to find the sub projects - give the folders
                              app.subdir = app
                              test.subdir  = test
                              
                              test.depends = app
                              

                              So far all I had to do was include the sources and headers in the pro files and the projects simply compiled. Please enlighten me if I'm missing something crucial.

                              update:
                              After dumpbin.exe Configuration.obj /disasm

                              ff927410-59f9-42c8-ac13-8b3dd1da0302-image.png

                              Even though we see the function is present in the obj file I still get following error

                              error: LNK2019: unresolved external symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in function ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".

                              What makes me wonder is the second part of the error message. But I guess thats just how Qt invokes all function calls for classes that inherit from QObject.

                              Christian EhrlicherC 1 Reply Last reply
                              0
                              • R Redman

                                @Christian-Ehrlicher
                                758ed0ad-61b4-482a-a62c-d0270c00b542-image.png

                                I think this should answer your question.

                                To elaborate further:
                                project.pro

                                
                                  SUBDIRS = \
                                            client \
                                            logger \
                                            service
                                			
                                  # where to find the sub projects - give the folders
                                  client.subdir = src/client
                                  logger.subdir  = src/logger
                                  service.subdir  = src/service
                                

                                service.pro

                                TEMPLATE = subdirs
                                
                                SUBDIRS = \
                                        app \
                                        test \
                                
                                # where to find the sub projects - give the folders
                                app.subdir = app
                                test.subdir  = test
                                
                                test.depends = app
                                

                                So far all I had to do was include the sources and headers in the pro files and the projects simply compiled. Please enlighten me if I'm missing something crucial.

                                update:
                                After dumpbin.exe Configuration.obj /disasm

                                ff927410-59f9-42c8-ac13-8b3dd1da0302-image.png

                                Even though we see the function is present in the obj file I still get following error

                                error: LNK2019: unresolved external symbol ""public: __cdecl Configuration::Configuration(class QObject *)" (??0Configuration@@QEAA@PEAVQObject@@@Z)" in function ""private: static void __cdecl TestConfig::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@TestConfig@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z)".

                                What makes me wonder is the second part of the error message. But I guess thats just how Qt invokes all function calls for classes that inherit from QObject.

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

                                Provide a minimal, compilable example. Don't use global globs to add files so we can see where exactly configuration.cpp is compiled into since you still (after 5 posts from me) failed to do so.

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

                                R 1 Reply Last reply
                                0
                                • Christian EhrlicherC Christian Ehrlicher

                                  Provide a minimal, compilable example. Don't use global globs to add files so we can see where exactly configuration.cpp is compiled into since you still (after 5 posts from me) failed to do so.

                                  R Offline
                                  R Offline
                                  Redman
                                  wrote on last edited by Redman
                                  #15

                                  @Christian-Ehrlicher Here you go
                                  https://file.io/dO55Ata0j6rm
                                  https://easyupload.io/bwbtos

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • R Redman

                                    @Christian-Ehrlicher Here you go
                                    https://file.io/dO55Ata0j6rm
                                    https://easyupload.io/bwbtos

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

                                    As I said - there is no library where you link your configuration.cpp - you only compile it directly to your app.
                                    So either also directly link it to your testcase or create a library and link the library to your app and your test.

                                    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
                                    3
                                    • R Redman

                                      @Christian-Ehrlicher
                                      #ifndef CONFIGURATION_H is present. I missed it while copying.

                                      My project structure:
                                      457dd8ba-0012-4297-8a30-71d318db39b5-image.png

                                      test.pro

                                      QT += testlib
                                      QT -= gui
                                      
                                      TARGET = TestService
                                      
                                      CONFIG += qt console warn_on depend_includepath testcase
                                      CONFIG -= app_bundle
                                      CONFIG -= debug_and_release
                                      
                                      TEMPLATE = app
                                      
                                      SOURCES += \
                                          $$files("$$PWD/src/*.cpp", true)
                                      
                                      S Offline
                                      S Offline
                                      SimonSchroeder
                                      wrote on last edited by
                                      #17

                                      @Redman said in Moc not generating functions:

                                      SOURCES +=
                                      $$files("$$PWD/src/*.cpp", true)

                                      If you insist on using this any further, you need to adjust your paths. To add configuration.cpp to your test.pro the path needs to be $$PWD/../app/src/*.cpp because $$PWD is service/test, but your sources are locate in service/app/src. $$PWD stands for "print working directory" and thus your working directory for app.pro and test.pro is different.

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        Redman
                                        wrote on last edited by
                                        #18

                                        Solution was to create SUBDIR .pro with lib and test subdirs. In lib I build a .dll from app and include the dll in test
                                        1c411b43-060b-429b-9589-0f8263271149-image.png

                                        1 Reply Last reply
                                        0
                                        • R Redman has marked this topic as solved on

                                        • Login

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