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. Force Moc to moc a file
Forum Updated to NodeBB v4.3 + New Features

Force Moc to moc a file

Scheduled Pinned Locked Moved General and Desktop
20 Posts 3 Posters 9.7k Views 3 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.
  • M Offline
    M Offline
    matze42
    wrote on last edited by
    #1

    Hi All,

    I've got an issue, that moc doesn't moc a file it should moc, under the following circumstances ( This is not exactly my situation, but explains resp. reproduces the problem):

    Suppose you have a macro defined in a header file - say foo.h:

    // in foo.h
    #define MAKE_TYPE(name) class name: public QObject { Q_OBJECT public: name() : QObject(0) {} };
    

    and a definition in the same file, like

    // in foo.h
    MAKE_TYPE(MyType)
    

    Then I can create an instance of MyType in any other file without any problems.

    // any file
    MyType t
    

    Do I move the definition in another file - say bar.h

    // in bar.h
    MAKE_TYPE(MyType)
    

    I get a couple of undefined references, when I create the Instance. This is because bar.h is not mocced ( is this the right word ?).
    To get it work, I have to put a dummy QObject in bar.h, like:

    // in bar.h
    class DummyQObj : public QObject { Q_OBJECT public: DummyQObj() : QObject(0) {} };
    MAKE_TYPE(MyType)
    

    The it's mocced and it works.
    So my question is, is there any way to tell the moc ( p.e. by means of a define ) to moc the file, although it thinks it shouldn't ?

    Or is this even a bug ?

    Thanks,
    Martin

    M 1 Reply Last reply
    0
    • M matze42

      Hi All,

      I've got an issue, that moc doesn't moc a file it should moc, under the following circumstances ( This is not exactly my situation, but explains resp. reproduces the problem):

      Suppose you have a macro defined in a header file - say foo.h:

      // in foo.h
      #define MAKE_TYPE(name) class name: public QObject { Q_OBJECT public: name() : QObject(0) {} };
      

      and a definition in the same file, like

      // in foo.h
      MAKE_TYPE(MyType)
      

      Then I can create an instance of MyType in any other file without any problems.

      // any file
      MyType t
      

      Do I move the definition in another file - say bar.h

      // in bar.h
      MAKE_TYPE(MyType)
      

      I get a couple of undefined references, when I create the Instance. This is because bar.h is not mocced ( is this the right word ?).
      To get it work, I have to put a dummy QObject in bar.h, like:

      // in bar.h
      class DummyQObj : public QObject { Q_OBJECT public: DummyQObj() : QObject(0) {} };
      MAKE_TYPE(MyType)
      

      The it's mocced and it works.
      So my question is, is there any way to tell the moc ( p.e. by means of a define ) to moc the file, although it thinks it shouldn't ?

      Or is this even a bug ?

      Thanks,
      Martin

      M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      HI and welcome to devnet

      @matze42 said:

      I get a couple of undefined references, when I create the Instance. This is because bar.h is not

      can you post the errors? Probably could be better to include <QObject> in the macro.

      BTW, Q_OBJECT is itself a macro; I suggest to define your macro in multi-line mode

      #define MAKE_TYPE(name) \
      #include <QObject>
      class name: public QObject \
      { \
      Q_OBJECT \
      public: \
        name(): QObject(0) {} \
      };
      
      1 Reply Last reply
      0
      • M Offline
        M Offline
        matze42
        wrote on last edited by
        #3

        Hi ,

        thanks for your answer.

        the error is

        error: undefined reference to `vtable for MyType'
        

        This is clear, as the moc file is not generated.

        ... and the original macro is of course defined in multiline mode, the posted example is only a condensed version.

        Additionally including <QObject> doesn't help ( btw: the way you posted it leads to preprocessor errors ).

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mcosta
          wrote on last edited by
          #4

          @matze42 said:

          Additionally including <QObject> doesn't help ( btw: the way you posted it leads to preprocessor errors ).

          Yep, I forgot \ at the end of the line.
          Can you post you .pro file? Are your headers in the HEADERS variable

          what happens if you run manually moc on this header file?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            matze42
            wrote on last edited by
            #5

            Hi,

            I simplified it and it looks like this:

            QT       += core
            QT       -= gui
            
            TARGET = moctest
            CONFIG   += console
            CONFIG   -= app_bundle
            
            TEMPLATE = app
            
            SOURCES += main.cpp \
                bar.cpp \
                foo.cpp
            
            HEADERS += \
                foo.h \
                bar.h
            
            // main.cpp
            #include "foo.h"
            #include "bar.h"
            
            #include <QCoreApplication>
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                MyType t;
                MyOtherType ot;
            
                return a.exec();
            }
            
            // foo.h
            #ifndef FOO_H
            #define FOO_H
            
            #include <QObject>
            
            #define MAKE_TYPE(name) \
                class name: public QObject \
               { \
                Q_OBJECT \
                public: name() : QObject(0) {}\
                };
            
            MAKE_TYPE(MyType)
            
            #endif // FOO_H
            
            // bar.h
            #ifndef BAR_H
            #define BAR_H
            
            #include "foo.h"
            #include <QObject>
            
            // With this uncommented it works
            //class DummyQObj : public QObject { Q_OBJECT public: DummyQObj() : QObject(0) {} };
            
            MAKE_TYPE(MyOtherType)
            
            #endif // BAR_H
            
            // foo.cpp
            #include "foo.h"
            
            // bar.cpp
            #include "bar.h"
            

            Just try it.
            ( Don't forget do completey cleanup your build directory after changing something, sometimes the object files are kept )

            Btw: Is it possible in this forum to attach files to posts ? Then I could attach my project directly.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mcosta
              wrote on last edited by
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • M Offline
                M Offline
                mcosta
                wrote on last edited by
                #7

                HI,

                my guess is that qmake force moc to be called only for header files where Q_OBJECT is used. qmake runs before the preprocessor so in your project it doesn't see the Q_OBJECT macro inside bar.h

                1 Reply Last reply
                0
                • M Offline
                  M Offline
                  matze42
                  wrote on last edited by
                  #8

                  Hi,

                  ... yes, that was my original question.
                  Quote from Post:
                  So my question is, is there any way to tell the moc ( p.e. by means of a define ) to moc the file, although it thinks it shouldn't ?

                  Of course I can put some Q_OBJECT somewhere on the top of the file, p.e.:

                  #ifdef SOMETHING_WHICH_DEFINITELY_IS_NOT_DEFINED_ELSEWHERE
                      Q_OBJECT
                  #endif
                  

                  where it's just there, but not evaluated.
                  But this looks pretty hacky and not nice.

                  I rather would like something like;

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

                    Hi,

                    Out of curiosity, which version of Qt are we talking about ?

                    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
                    0
                    • M Offline
                      M Offline
                      matze42
                      wrote on last edited by
                      #10

                      Hi,

                      thanks for asking - i missed that:
                      Qt 5.4 on Xubuntu 14.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        matze42
                        wrote on last edited by
                        #11

                        Ok,

                        I understand, that qmakedoes not recognize ( and probably even cannot - as it supposed doesn't expand macros) that the concerning file needs to get mocced.
                        Is there any ways then to add something in the pro file to tell qmake, that the file needs to get mocced ? p.e.:

                        moc += bar.h
                        
                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by SGaist
                          #12

                          qmake is a project manager so indeed it doesn't include macro expansion. moc on the other end (since Qt 5) should expand macro properly.

                          I just tested your code on OS X and it worked

                          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
                          0
                          • M Offline
                            M Offline
                            matze42
                            wrote on last edited by
                            #13

                            Hi SGaist,

                            thanx for caring about this issue,
                            Did yout test it without the Q_OBJECT pseudo code commented or uncommented ?

                            In the meanwhile I started to redesign this part of our software (we are talking about 70-80 type definitions in about 7-8 namespaces resp. files ) - but anyway it would be quite interesting ( at least for me ) to know, if such kind of constructs make sense in the Qt world or not.

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

                              I've removed/re-added it and it still builds. Q_OBJECT is useful when you implement e.g. signals and slots

                              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
                              0
                              • M Offline
                                M Offline
                                matze42
                                wrote on last edited by
                                #15

                                Strange !

                                Seems, that my qmake implementation is different from yours.

                                Reproducable it builds if I have the Q_OBJECT uncommented, and it gives me the linker errors if i have it commented.

                                So this led me to the assumption, that a (uncommented) Q_OBJECT occurance in a header file is the hint for qmake to mark it to be mocced.

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

                                  Which version of qmake are you using ?

                                  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
                                  0
                                  • M Offline
                                    M Offline
                                    matze42
                                    wrote on last edited by
                                    #17

                                    Qt 5.4.0 GCC 64bit on Xubuntu 14 64bit.

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

                                      Are you calling qmake through Qt Creator or on the command line ?

                                      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
                                      0
                                      • M Offline
                                        M Offline
                                        matze42
                                        wrote on last edited by
                                        #19

                                        By QtCreator

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

                                          Did you re-try with Qt 5.5 ?

                                          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
                                          0

                                          • Login

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