Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-81792

Multiple Q_NAMESPACE for the same namespace in different files

    XMLWordPrintable

    Details

    • Platform/s:
      All

      Description

      I have two enums. They are in the same namespace, but different header files. To make them available to Qt meta type system, I try this:

      C1.h
      #include <QObject>
      namespace SW {
          Q_NAMESPACE
          enum class Enum1 {A, B};
          Q_ENUM_NS(Enum1)
      }
      
      C2.h
      #include <QObject>
      namespace SW {
          Q_NAMESPACE
          enum class Enum2 {A, B};
          Q_ENUM_NS(Enum2)
      }
      
      main.cpp
      #include "C1.h"
      #include "C2.h"
      int main(int argc, char *argv[]) {
      }
      
      • With the above I get duplicate symbol linker error. Because both moc_C1.o and moc_C2.o defined the staticMetaObject that result from Q_NAMESPACE
      • Both headers must contain Q_NAMESPACE. Otherwise moc complains "Error: Namespace declaration lacks Q_NAMESPACE macro."
      • If I have only one of C1.h or C2.h, it works.
      • If I move content of C2.h into C1.h, it also works

      The core of the problem seems to be that with multiple header files, the toolchain doesn't know how to define only one copy of the namespace's meta object stuff. 

      The following looks like a reasonable effort:

      SW_MetaDef.h
      #include <QObject>
      namespace SW {
          Q_NAMESPACE
      }
      
      C1.h
      #include "SW_MetaDef.h"
      namespace SW {
          enum class Enum1 {A, B};
          Q_ENUM_NS(Enum1)
      }
      
      C2.h
      #include "SW_MetaDef.h"
      namespace SW {
          enum class Enum2 {A, B};
          Q_ENUM_NS(Enum2)
      }
      

      With the above:

      • moc_SW_MetaDef.cpp defines the only copy of SW namespace's meta object stuff
      • The staticMetaObject referenced by Q_ENUM_NS is visible. So should be no compiler error there
      • But moc would output "Error: Namespace declaration lacks Q_NAMESPACE macro." Is moc being too strict? Would the above work if we disable the "Q_NAMESPACE present" check in moc?

      Related issue: QTBUG-68611

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            twj T W
            Votes:
            4 Vote for this issue
            Watchers:
            6 Start watching this issue

              Dates

              Created:
              Updated:

                Gerrit Reviews

                There are no open Gerrit changes