【发布时间】:2015-04-06 13:04:27
【问题描述】:
我有一个基类 (QIndicator),我想在 DLL 中实现派生类。 Visual Studio 2012 中示例派生类的 DLL 项目具有以下代码:
带有基类的头文件
#ifndef _DLL_COMMON_INDICATOR_
#define _DLL_COMMON_INDICATOR_
// define the DLL storage specifier macro
#if defined DLL_EXPORT
#define DECLDIR __declspec(dllexport)
#else
#define DECLDIR __declspec(dllimport)
#endif
class QIndicator
{
private:
int x;
int y;
};
extern "C"
{
// declare the factory function for exporting a pointer to QIndicator
DECLDIR QIndicator * __stdcall getIndicatorPtr(void);
}
#endif
带有派生类的源文件
#define DLL_EXPORT
#include "indicator.h"
class QIndicatorDer : public QIndicator
{
public:
QIndicatorDer (void) : QIndicator(){};
~QIndicatorDer (void){};
private:
// list of QIndicatorDer parameters
int x2;
int y2;
};
extern "C"
{
DECLDIR QIndicator * __stdcall getIndicatorPtr(void)
{
return new QIndicatorDer();
};
}
我遇到的问题是,在成功构建后,生成的 DLL 文件不包含导出的 getIndicatorPtr 函数(如 DependencyWalker 所示)。我检查了 dllexport 关键字是否正确传播到 getIndicatorPtr 的声明中,并且确实如此。
另一个有趣的问题是我在几个月前创建的另一个 DLL 项目中已经有另一个类似的派生类。这个较旧的项目基本相同,并且在那里一切正常。我检查了旧项目和当前项目的所有属性,它们看起来是相同的。所以我没有想法,为什么我不能让getIndicatorPtr 导出。
非常感谢任何帮助, 丹尼尔
【问题讨论】:
-
你为什么在这里使用
__stdcall? -
QIndicator类未导出。你也需要导出这个类。 -
老实说,在教程中找到了它(尝试互联网上知道的所有可能性来解释这个 DLL 行为)..没有它也一样。
-
@user2225104 错误。只要
QIndicator不包含任何代码,他就不需要导出它。它只包含数据成员,所以包含这个类的头部就足以正确使用它。 -
我有许多其他的基派生类实现了逻辑。基类无处导出,DLL 逻辑相同,一切正常。
标签: c++ visual-studio-2012 dll