Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Get warnings by using a QObject class in DLL



  • 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?


  • Lifetime Qt Champion

    @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.



  • @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


  • Lifetime Qt Champion

    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.



  • @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!


Log in to reply