免责声明:在 Linux 上一切正常。在 Windows 上,我最近从 MinGW 更改为 MSVC2012,因为我无法正确读取 MP3(请参阅 Make the WMF plugin compile with MinGW.)
在我的项目中,我有:
- 核心(DLL)
- 另一个使用 Core 构建的 DLL
- 媒体播放器(Qt 应用程序,同时使用 DLL 和加载插件)
为了让我的项目跨平台,我还将 Windows 特定功能 ( progress bar, thumbnail buttons ) 提取到第 3 方插件中。事实上,我开始编写一个插件管理器,它在运行时加载/卸载插件并且无需重新启动应用程序,它工作正常。
但是自从我切换到 MSVC 之后,我就不能再构建我的插件了。我面对:
C4273: 'staticMetaObject' : inconsistent dll linkage
我不知道如何进行...我有以下结构:
在 MainProject\Core\Interfaces 中
class MIAMCORE_LIBRARY MediaPlayerPluginInterface : public BasicPluginInterface
{
public:
virtual ~MediaPlayerPluginInterface() {}
virtual void setMediaPlayer(QWeakPointer<MediaPlayer>) = 0;
virtual bool providesView() const = 0;
virtual void toggleViews(QWidget *) {}
};
在插件.h中
class Minimode : public QWidget, public MediaPlayerPluginInterface
{
Q_OBJECT
Q_PLUGIN_METADATA(IID MediaPlayerPluginInterface_iid)
Q_INTERFACES(MediaPlayerPluginInterface)
private:
Ui::ConfigForm _ui;
QWeakPointer<MediaPlayer> _mediaPlayer;
bool _startMoving;
QPoint _pos, _globalPos;
public:
explicit Minimode();
virtual ~Minimode();
inline virtual QString name() const { return "Minimode"; }
inline virtual QString version() const { return "1.0"; }
inline virtual bool providesView() const { return true; }
virtual QWidget* configPage();
virtual void setMediaPlayer(QWeakPointer<MediaPlayer> mediaPlayer);
virtual void toggleViews(QWidget *view);
protected:
/** Redefined to be able to drag this widget on screen. */
void mouseMoveEvent(QMouseEvent *e);
/** Redefined to be able to drag this widget on screen. */
void mouseReleaseEvent(QMouseEvent *e);
void mousePressEvent(QMouseEvent *e);
private:
void applyColorToStandardIcon(QPushButton *button);
};
我的插件.pro
QT += widgets multimedia
TARGET = $$qtLibraryTarget(mini-mode)
TEMPLATE = lib
MiamPlayerBuildDirectory = C:\dev\Miam-Player\build\MiamPlayer
DEFINES += MINIMODE_MIAMPLUGIN
CONFIG += c++11
CONFIG(debug, debug|release) {
target.path = $$MiamPlayerBuildDirectory\debug\plugins
LIBS += -Ldebug -lMiamCore
}
INSTALLS += target
Miamcore_global.h
#ifndef MIAMCORE_GLOBAL_H
#define MIAMCORE_GLOBAL_H
#include <QtCore/QtGlobal>
#if defined(MIAMCORE_LIBRARY)
#undef MIAMCORE_LIBRARY
#define MIAMCORE_LIBRARY Q_DECL_EXPORT
#else
#define MIAMCORE_LIBRARY Q_DECL_IMPORT
#endif
#endif // MIAMCORE_GLOBAL_H
我已经用宏 MIAMCORE_LIBRARY 和另一个 MINIMODE_PLUGIN 尝试了各种排列,但都没有奏效(在类和 Minimode 之间,但上面没有显示)。我应该在我的 *.pro 文件中添加特定的关键字吗?
最佳答案
您的宏似乎对于导出和导入是错误的。
假设您不需要为插件静态构建,这在某种程度上是真实的期望,因为它们是动态库,您应该像下面这样使用宏。
另请注意,您忘记包含定义导入和导出宏的全局 Qt header 。
Miamcore_global.h
#ifndef MIAMCORE_GLOBAL_H
#define MIAMCORE_GLOBAL_H
#include <QtCore/qglobal.h>
#ifdef MINIMODE_MIAMPLUGIN
# define MIAMCORE_LIBRARY Q_DECL_EXPORT
#else
# define MIAMCORE_LIBRARY Q_DECL_IMPORT
#endif
#endif // MIAMCORE_GLOBAL_H
然后您需要在项目文件中指定以下定义:
你的插件.pro
...
DEFINES += MINIMODE_MIAMPLUGIN
...
这种设计在我们的 Qt 4 插件架构以及 Windows 和 Unix 上的 Qt 5 中运行良好。
此处重要的是 Windows。如果没有这个,在 Linux 上会很好,但对于 Windows,您需要确保在构建插件时导出插件,但在实际使用时导入。
这就是为什么您需要将所需的指定传递给此构建时将其导出的项目文件。一次,你的用户要用这个插件,这个定义就没有了,然后导入。
关于c++ - 如何使用另一个 DLL 为 Qt 制作第三方插件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20815511/