【问题标题】:No functions get exported into DLL despite proper dllExport - Visual Studio尽管 dllExport 正确,但没有函数导出到 DLL - Visual Studio
【发布时间】: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


【解决方案1】:

那是因为它没有被导出。为什么?

__declspec 说明符只能放在函数的声明中,而不是定义中。另外,请避免使用#define DLL_EXPORT 之类的内容。预处理器定义应在项目属性 (MSVC) 或命令行选项中定义(例如,GCC 中的-D)。

看看你的代码:

标题

extern "C"      
{
    DECLDIR QIndicator * __stdcall getIndicatorPtr(void);
}

当编译器解析此标头时,将DECLDIR 视为dllimport(因为您在.cpp 中定义了DLL_EXPORT)。然后在.cpp,突然出现dllexport。使用哪一种?第一个。

所以,留下你的标题(没关系),但改变你的来源:

//#define DLL_EXPORT -> remove this!

#include "indicator.h"

class QIndicatorDer : public QIndicator
{
    //...
};

extern "C"     
{
    /* DECLDIR -> and this! */ QIndicator * __stdcall getIndicatorPtr(void)
    {
        return new QIndicatorDer();
    };
}

然后,转到项目属性(我假设您使用 Visual Studio)然后 C/C++ -> Preprocessor -> Preprocessor Definitions 并添加 DLL_EXPORT=1

应该可以的。

【讨论】:

  • 马特乌斯,谢谢!该代码在您更改后有效。向波兰致敬!
  • __declspec 在定义中是正确的。将宏定义移动到项目属性是这里的重要一步。
猜你喜欢
  • 2021-09-17
  • 1970-01-01
  • 1970-01-01
  • 2013-03-01
  • 2020-01-22
  • 2018-03-23
  • 1970-01-01
  • 2012-04-14
相关资源
最近更新 更多