Export QObject-based class to DLL

陌路散爱 提交于 2019-12-10 15:16:06

问题


I'm composing a class which derives from QObject, and I want to export this class into a DLL file so other applications can use it. But I got some mysterious problem here:

The code is shown below:

mydll.h:

 #ifndef MYDLL_H
 #define MYDLL_H

 #include "mydll_global.h"
 #include <QObject>
 #include <QDebug>

 class MYDLLSHARED_EXPORT MyDll : public QObject
 {
     Q_OBJECT
  public:
     explicit MyDll(QObject * parent = 0);

     void test() const;
 };

 #endif // MYDLL_H

mydll_global.h:

 #ifndef MYDLL_GLOBAL_H
 #define MYDLL_GLOBAL_H

 #include <QtCore/qglobal.h>

 #if defined(MYDLL_LIBRARY)
 #  define MYDLLSHARED_EXPORT Q_DECL_EXPORT
 #else
 #  define MYDLLSHARED_EXPORT Q_DECL_IMPORT
 #endif

 #endif // MYDLL_GLOBAL_H

mydll.cpp:

 #include "mydll.h"

 MyDll::MyDll(QObject * parent) :
     QObject(parent)
 {
 }

 void MyDll::test() const {
     qDebug() << "Hello from dll!";
 }

and the dll is used in another application. The dll is compiled successfully. I've add LIBS += "myDll.dll" in the .pro file of the application using this dll, and I've copied myDll.dll to the working directory of the application.

The compiler reports:

 C4273: "MyDll::qt_static_metacall" : inconsistent dll linkage.
 C2491: "MyDll::staticMetaObject": definition of dllimport static data member not allowed

What's the problem here?


回答1:


Your code for mydll_global.h checks whether MYDLL_LIBRARY is defined, but none of the code you have posted defines MYDLL_LIBRARY. Is this declared in a file that you have not shared on the question? If not, you need to add a #define MYDLL_LIBRARY in your build project, or your PCH.



来源:https://stackoverflow.com/questions/19238693/export-qobject-based-class-to-dll

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!