将基于QObject的类导出到DLL [英] Export QObject-based class to DLL

查看:1517
本文介绍了将基于QObject的类导出到DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写一个派生自QObject的类,我想把这个类导出成一个DLL文件,以便其他的应用可以使用它。但是我在这里有一些神秘的问题:



代码如下所示:



mydll.h: p>

  #ifndef MYDLL_H 
#define MYDLL_H

#includemydll_global.h
#include< QObject>
#include< QDebug>

class MYDLLSHARED_EXPORT MyDll:public QObject
{
Q_OBJECT
public:
显式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定义(MYDLL_LIBRARY)
#define MYDLLSHARED_EXPORT Q_DECL_EXPORT
#else
#define MYDLLSHARED_EXPORT Q_DECL_IMPORT
#endif

#endif // MYDLL_GLOBAL_H

mydll.cpp:



pre $ #includemydll.h

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

void MyDll :: test()const {
qDebug()<< 你好,从dll!;
}

,并在其他应用程序中使用dll。 dll被编译成功。我在使用此DLL的应用程序的 .pro 文件中添加了 LIBS + =myDll.dll我已将 myDll.dll 复制到应用程序的工作目录。



编译器报告: p>

  C4273:MyDll :: qt_static_metacall:不一致的dll链接。 
C2491:MyDll :: staticMetaObject:dllimport的定义静态数据成员不允许

这里有什么问题?

解决方案

您的代码 mydll_global.h 是否定义了 MYDLL_LIBRARY ,但没有发布的代码定义 MYDLL_LIBRARY 。这是在一个文件中声明的,您尚未就此问题分享?如果没有,您需要在构建项目或PCH中添加一个 #define MYDLL_LIBRARY


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?

解决方案

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.

这篇关于将基于QObject的类导出到DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆