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. The Qt MOC and Preprocessor

The Qt MOC and Preprocessor

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 4.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
    ConcordSpark
    wrote on 27 May 2017, 08:22 last edited by
    #1

    Hi all!

    I understand that the MOC tool reads any class containing Q_OBJECT (as well as any other Qt extensions such as 'signals' and 'slots') and generates a separate source file containing the appropriate member function definitions.

    I also know that the MOC tool runs before the preprocessor.

    What I'm wondering is if after the MOC tool generates the definitions, the preprocessor goes in and expands the Q_OBJECT macro into the source file (in order to get the function prototypes), or does the MOC tool take care of expanding macros as well?

    I'm a bit confused on this point as I've heard that starting with Qt 5.0, the MOC is capable of macro expansion... Does the preprocessor still run after the MOC then?

    Thanks for your time!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on 27 May 2017, 11:06 last edited by Chris Kawa
      #2

      Moc is a pre-compilation step. It is a separate tool, does not communicate with the compiler in any way and does not modify the files it processes. All its work results are placed in a separate file. Basically it just reads some code and generates more files for the compiler to compile.

      So the basic toolchain without moc is:
      compiler foo.h foo.cpp ->foo.obj
      linker foo.obj -> foo.exe
      and with moc it's
      moc foo.h -> moc_foo.cpp
      compiler foo.h foo.cpp -> foo.obj
      compiler foo.h moc_foo.cpp -> moc_foo.obj
      linker foo.obj moc_foo.obj -> foo.exe

      As for the "moc understands macros" part - it just means that it runs its own pre-processing pass to make sure macros are respected ie. something like this

      #if 0
         Q_OBJECT
      #endif
      

      will be understood and no code will be generated, where previously it would generate unconditionally. It has nothing to do with macro expansion the compiler does when it runs.

      Q_OBJECT is special only for moc. It understands what it is and generates members implementations in the moc_foo.cpp. For compiler Q_OBJECT is just a regular macro and is expanded as usual to crate the members declarations while processing foo.h.

      C 1 Reply Last reply 27 May 2017, 14:56
      4
      • C Chris Kawa
        27 May 2017, 11:06

        Moc is a pre-compilation step. It is a separate tool, does not communicate with the compiler in any way and does not modify the files it processes. All its work results are placed in a separate file. Basically it just reads some code and generates more files for the compiler to compile.

        So the basic toolchain without moc is:
        compiler foo.h foo.cpp ->foo.obj
        linker foo.obj -> foo.exe
        and with moc it's
        moc foo.h -> moc_foo.cpp
        compiler foo.h foo.cpp -> foo.obj
        compiler foo.h moc_foo.cpp -> moc_foo.obj
        linker foo.obj moc_foo.obj -> foo.exe

        As for the "moc understands macros" part - it just means that it runs its own pre-processing pass to make sure macros are respected ie. something like this

        #if 0
           Q_OBJECT
        #endif
        

        will be understood and no code will be generated, where previously it would generate unconditionally. It has nothing to do with macro expansion the compiler does when it runs.

        Q_OBJECT is special only for moc. It understands what it is and generates members implementations in the moc_foo.cpp. For compiler Q_OBJECT is just a regular macro and is expanded as usual to crate the members declarations while processing foo.h.

        C Offline
        C Offline
        ConcordSpark
        wrote on 27 May 2017, 14:56 last edited by
        #3

        @Chris-Kawa

        Thanks so much! I had a feeling it was like this, but just wasn't sure :) Thanks again!

        E 1 Reply Last reply 27 May 2017, 16:58
        0
        • C ConcordSpark
          27 May 2017, 14:56

          @Chris-Kawa

          Thanks so much! I had a feeling it was like this, but just wasn't sure :) Thanks again!

          E Offline
          E Offline
          Eeli K
          wrote on 27 May 2017, 16:58 last edited by
          #4

          @ConcordSpark https://woboq.com/blog/moc-myths.html gives some interesting details.

          C 1 Reply Last reply 28 May 2017, 07:28
          2
          • E Offline
            E Offline
            Eddy
            wrote on 27 May 2017, 18:20 last edited by
            #5

            @Chris-Kawa
            Your post reads like poetry ;-)

            Eddy

            Qt Certified Specialist
            www.edalsolutions.be

            1 Reply Last reply
            0
            • E Eeli K
              27 May 2017, 16:58

              @ConcordSpark https://woboq.com/blog/moc-myths.html gives some interesting details.

              C Offline
              C Offline
              ConcordSpark
              wrote on 28 May 2017, 07:28 last edited by
              #6

              @Eeli-K Thanks for that!

              1 Reply Last reply
              0

              4/6

              27 May 2017, 16:58

              • Login

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