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. dllexport produces incorrect warning from Clang Code Model
QtWS25 Last Chance

dllexport produces incorrect warning from Clang Code Model

Scheduled Pinned Locked Moved Solved General and Desktop
clang analyserdllexport
9 Posts 3 Posters 3.5k 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.
  • Resurr3ctionR Offline
    Resurr3ctionR Offline
    Resurr3ction
    wrote on last edited by Resurr3ction
    #1

    When exporting a class derived from QObject from a shared library using the ***SHARED_EXPORT macro that expands to dllexport/dllimport the Clang Code Model issues following warning on a class:

    warning: 'SomeClass' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit
    

    The minimal example:

    In *.pro

    DEFINES += SOMECLASSLIB_LIBRARY
    

    In SomeClass.h

    #if defined(SOMECLASSLIB_LIBRARY)
    #  define SOMECLASSLIB_EXPORT Q_DECL_EXPORT
    #else
    #  define SOMECLASSLIB_EXPORT Q_DECL_IMPORT
    #endif
    
    class SOMECLASSLIB_EXPORT SomeClass : public QObject
    {
        Q_OBJECT
    public:
        virtual ~SomeClass(); //defined in cpp
    };
    

    In SomeClass.cpp

    SomeClass::~SomeClass() {}
    

    Is this a bug in Clang, Qt, Windows or something else? The issue is the export macro.

    A 1 Reply Last reply
    0
    • Resurr3ctionR Resurr3ction

      When exporting a class derived from QObject from a shared library using the ***SHARED_EXPORT macro that expands to dllexport/dllimport the Clang Code Model issues following warning on a class:

      warning: 'SomeClass' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit
      

      The minimal example:

      In *.pro

      DEFINES += SOMECLASSLIB_LIBRARY
      

      In SomeClass.h

      #if defined(SOMECLASSLIB_LIBRARY)
      #  define SOMECLASSLIB_EXPORT Q_DECL_EXPORT
      #else
      #  define SOMECLASSLIB_EXPORT Q_DECL_IMPORT
      #endif
      
      class SOMECLASSLIB_EXPORT SomeClass : public QObject
      {
          Q_OBJECT
      public:
          virtual ~SomeClass(); //defined in cpp
      };
      

      In SomeClass.cpp

      SomeClass::~SomeClass() {}
      

      Is this a bug in Clang, Qt, Windows or something else? The issue is the export macro.

      A Offline
      A Offline
      ambershark
      wrote on last edited by
      #2

      @Resurr3ction What that warning is saying is that there are no functions that aren't defined inline. More than likely the compiler optimized your no-code destructor into an inline function.

      Try changing it's definition in the cpp file to:

      SomeClass::~SomeClass()
      {
      }
      

      And if that fails, add some code or another function. It should generate the vtable just fine as long as your class actually does something to export.

      My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

      kshegunovK 1 Reply Last reply
      0
      • Resurr3ctionR Offline
        Resurr3ctionR Offline
        Resurr3ction
        wrote on last edited by
        #3

        Unfortunately this is not the problem. Regardless of how many virtual functions are defined (or not) the warning only goes away if I remove the exporting macro. Therefore I conclude that the problem is with Clang handling the export macro and not the vtable being emitted every time. Unless of course exporting from shared library meant that vtable IS emitted in every translation unit. Then the warning would be technically correct but there is nothing one can do about it...

        kshegunovK 1 Reply Last reply
        0
        • Resurr3ctionR Resurr3ction

          Unfortunately this is not the problem. Regardless of how many virtual functions are defined (or not) the warning only goes away if I remove the exporting macro. Therefore I conclude that the problem is with Clang handling the export macro and not the vtable being emitted every time. Unless of course exporting from shared library meant that vtable IS emitted in every translation unit. Then the warning would be technically correct but there is nothing one can do about it...

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @Resurr3ction
          Add an out-of-line method before the destructor, that should make it disappear I think. e.g.

          class SOMECLASSLIB_EXPORT SomeClass : public QObject
          {
              Q_OBJECT
          public:
              SomeClass(QObject * = Q_NULLPTR);
              virtual ~SomeClass();
          };
          

          myclass.cpp

          SomeClass(QObject * parent)
              : QObject(parent)
          {
              // Just the trivial constructor
          }
          
          SomeClass::~SomeClass()
          {
          }
          

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          4
          • Resurr3ctionR Offline
            Resurr3ctionR Offline
            Resurr3ction
            wrote on last edited by
            #5

            @kshegunov Well yes, that is it. I used inherited constructor so first real method was the virtual destructor. Thank you!

            A 1 Reply Last reply
            0
            • Resurr3ctionR Resurr3ction

              @kshegunov Well yes, that is it. I used inherited constructor so first real method was the virtual destructor. Thank you!

              A Offline
              A Offline
              ambershark
              wrote on last edited by
              #6

              @Resurr3ction

              And if that fails, add some code or another function. It should generate the vtable just fine as long as your class actually does something to export.

              So like I said add some code/another function for your vtable to generate properly, lol. :) I just wasn't as succinct as @kshegunov.

              My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

              1 Reply Last reply
              0
              • A ambershark

                @Resurr3ction What that warning is saying is that there are no functions that aren't defined inline. More than likely the compiler optimized your no-code destructor into an inline function.

                Try changing it's definition in the cpp file to:

                SomeClass::~SomeClass()
                {
                }
                

                And if that fails, add some code or another function. It should generate the vtable just fine as long as your class actually does something to export.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #7

                @ambershark said in dllexport produces incorrect warning from Clang Code Model:

                More than likely the compiler optimized your no-code destructor into an inline function.

                This, however, is incorrect. Virtual methods can't be inlined, ever.

                Read and abide by the Qt Code of Conduct

                A 1 Reply Last reply
                1
                • kshegunovK kshegunov

                  @ambershark said in dllexport produces incorrect warning from Clang Code Model:

                  More than likely the compiler optimized your no-code destructor into an inline function.

                  This, however, is incorrect. Virtual methods can't be inlined, ever.

                  A Offline
                  A Offline
                  ambershark
                  wrote on last edited by
                  #8

                  @kshegunov said in dllexport produces incorrect warning from Clang Code Model:

                  @ambershark said in dllexport produces incorrect warning from Clang Code Model:

                  More than likely the compiler optimized your no-code destructor into an inline function.

                  This, however, is incorrect. Virtual methods can't be inlined, ever.

                  I did not know that. Then again I don't think I've ever tried to inline a virtual as it really wouldn't make sense. Learn something new everyday. :)

                  My L-GPL'd C++ Logger github.com/ambershark-mike/sharklog

                  kshegunovK 1 Reply Last reply
                  0
                  • A ambershark

                    @kshegunov said in dllexport produces incorrect warning from Clang Code Model:

                    @ambershark said in dllexport produces incorrect warning from Clang Code Model:

                    More than likely the compiler optimized your no-code destructor into an inline function.

                    This, however, is incorrect. Virtual methods can't be inlined, ever.

                    I did not know that. Then again I don't think I've ever tried to inline a virtual as it really wouldn't make sense. Learn something new everyday. :)

                    kshegunovK Offline
                    kshegunovK Offline
                    kshegunov
                    Moderators
                    wrote on last edited by kshegunov
                    #9

                    I meant that you can write as an inline (in the class declaration) or not, but the compiler can never inline the code (as it's a call trough the virtual table). And of course virtual and inline are mutually exclusive as modifiers in the declaration.

                    Basically:

                    class A
                    {
                        virtual void a() {}
                    }
                    

                    is the same as

                    class A
                    {
                        virtual void a();
                    }
                    
                    void A::a()
                    {
                    }
                    

                    Read and abide by the Qt Code of Conduct

                    1 Reply Last reply
                    3

                    • Login

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