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. Get warnings by using a QObject class in DLL

Get warnings by using a QObject class in DLL

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.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.
  • B Offline
    B Offline
    billliu
    wrote on last edited by
    #1

    I created a DLL and used it in another project. I found all DLL classes which inherit from QObject cause warings about dllimport, so I just made a very simple class called "MyLib" to create a DLL and to see if it will still hapen.

    In mylib.h:

    #ifndef MYLIB_H
    #define MYLIB_H
    
    #include "MyLib_global.h"
    #include <QObject>
    
    class MYLIB_EXPORT MyLib :  public QObject
    {
         Q_OBJECT
    public:
        MyLib();
        void Hello();
    };
    
    #endif // MYLIB_H
    

    When the class inherits from QObject, it can run but the warnings show up:

    warning: 'static void MyLib::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    debug\moc_mylib.cpp:55:6: warning: 'static void MyLib::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    void MyLib::qt_static_metacall(QObject _o, QMetaObject::Call _c, int _id, void _a)
    ^~~~~
    warning: 'MyLib::staticMetaObject' redeclared without dllimport attribute after being referenced with dll linkage
    debug\moc_mylib.cpp:63:26: warning: 'MyLib::staticMetaObject' redeclared without dllimport attribute after being referenced with dll linkage
    const QMetaObject MyLib::staticMetaObject = { {
    ^~~~~~~~~~~~~~~~
    warning: 'virtual const QMetaObject
    MyLib::metaObject() const' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    debug\moc_mylib.cpp:74:20: warning: 'virtual const QMetaObject
    MyLib::metaObject() const' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    const QMetaObject MyLib::metaObject() const
    ^~~~~
    warning: 'virtual void
    MyLib::qt_metacast(const char
    )' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    debug\moc_mylib.cpp:79:7: warning: 'virtual void* MyLib::qt_metacast(const char*)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    void MyLib::qt_metacast(const char _clname)
    ^~~~~
    warning: 'virtual int MyLib::qt_metacall(QMetaObject::Call, int, void
    )' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    debug\moc_mylib.cpp:87:5: warning: 'virtual int MyLib::qt_metacall(QMetaObject::Call, int, void**)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    int MyLib::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
    ^~~~~

    If I remove QObject and test again, the warnings will disappear. I am using Qt6.0.
    Does anyone know how to solve this problem?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @billliu said in Get warnings by using a QObject class in DLL:

      MYLIB_EXPORT

      Your definition of the macro is wrong when you use the library. Please read the docs.

      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
      1
      • B Offline
        B Offline
        billliu
        wrote on last edited by billliu
        #3

        @Christian-Ehrlicher said in Get warnings by using a QObject class in DLL:

        Your definition of the macro is wrong when you use the library. Please read the docs.

        Do you mean I should define Q_DECL_EXPORT and Q_DECL_IMPORT correctly?
        I think they are correct because a header file created by Qt defines them.
        When I move the cursor to "MYLIB_EXPORT", it also seems correct, as shown below.

        My shared library:
        751622cc-c2d6-4e59-b345-5ff689606b72-image.png

        My client:
        df6d9e3a-c82a-4613-9b0b-d2763bd1d5db-image.png

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You must not add mylib.h to your pro-file of MyTest.pro - it does not belong to this project and triggers a moc run for it.

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

          B 1 Reply Last reply
          1
          • Christian EhrlicherC Christian Ehrlicher

            You must not add mylib.h to your pro-file of MyTest.pro - it does not belong to this project and triggers a moc run for it.

            B Offline
            B Offline
            billliu
            wrote on last edited by billliu
            #5

            @Christian-Ehrlicher said in Get warnings by using a QObject class in DLL:

            You must not add mylib.h to your pro-file of MyTest.pro - it does not belong to this project and triggers a moc run for it.

            I think it's right.
            Thanks for your help!

            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