尽管具有正确的dllExport,也没有函数导出到DLL中-Visual Studio [英] No functions get exported into DLL despite proper dllExport - Visual Studio

查看:161
本文介绍了尽管具有正确的dllExport,也没有函数导出到DLL中-Visual Studio的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基类( QIndicator ),我想在DLL中实现派生类。一个示例派生类的Visual Studio 2012中的DLL项目具有以下代码:

I have a base class (QIndicator) and I want to implement derived classes in DLLs. The DLL project in Visual Studio 2012 for a sample derived class has the following code:

基类的头文件

#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 的声明中,并且确实如此。

The problem I have is that upon successful build, the produced DLL file does not contain the exported getIndicatorPtr function (as shown by DependencyWalker). I checked whether the dllexport keyword gets propagated properly into the declaration of getIndicatorPtr and it does.

另一个有趣的问题是,我几个月前创建的另一个DLL项目中已经有了另一个这样的派生类。这个较旧的项目基本上是相同的,并且一切正常。我检查了旧项目和当前项目的所有属性,它们似乎相同。因此,我没了主意,为什么我无法导出 getIndicatorPtr

Another interesting problem is that I already have another derived class like this, in another DLL project, that I created some months ago. This older project is basically the same and everything works well there. I checked all properties of both the old and the current projects, and they seem identical. So I ran out of ideas, why I can't get getIndicatorPtr to export.

任何帮助深表感谢,
Daniel

Any help is much appreciated, Daniel

推荐答案

这是因为未导出。为什么?

That's because it's not being exported. Why?

__ declspec 说明符只能放在函数的声明中,不是它的定义。另外,请避免使用 #define DLL_EXPORT 之类的方法。预处理程序定义应该在项目属性(MSVC)或命令行选项(例如,GCC中的 -D )中定义。

__declspec specifier should only be placed in the declaration of a function, not it's definition. Also, avoid something like #define DLL_EXPORT. Preprocessor definitions should either defined in project properties (MSVC) or command line option (-D in GCC, for example).

查看您的代码:

标题

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

当编译器解析此标头时,会看到 DECLDIR 作为 dllimport (因为您在 .cpp中定义了 DLL_EXPORT )。然后在 .cpp 中,它突然显示为 dllexport 。使用哪一个?第一个。

When compiler parses this header, is sees DECLDIR as dllimport (because you define DLL_EXPORT in .cpp). Then in .cpp, it suddenly appears as dllexport. Which one is used? The first one.

因此,保留标题(很好),但是更改源:

So, leave your header (it's fine), but change your source:

//#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 ++ -> 预处理器-> 预处理器定义和在此处添加 DLL_EXPORT = 1

Then, go to project properties (I assume you use Visual Studio) and then C/C++ -> Preprocessor -> Preprocessor Definitions and add there DLL_EXPORT=1.

应该可以。

这篇关于尽管具有正确的dllExport,也没有函数导出到DLL中-Visual Studio的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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