2015-04-06 39 views
0

我有一個基類(QIndicator),我想在DLL中實現派生類。在Visual Studio 2012 DLL項目用於樣本的派生類具有以下的代碼:與基類儘管正確的dllExport沒有功能導出到DLL - Visual Studio

頭文件

#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導出。

任何幫助是非常讚賞, 丹尼爾

+0

爲什麼在這裏使用'__stdcall'? – BitTickler 2015-04-06 13:10:35

+0

「QIndicator」類未導出。您也需要導出課程。 – BitTickler 2015-04-06 13:12:23

+0

在教程中發現它,誠實地(嘗試所有知道互聯網來解釋這個DLL行爲)..沒有它的工作。 – 2015-04-06 13:12:52

回答

2

那是因爲它沒有被導出。爲什麼?

__declspec說明符應該只放在聲明的一個函數中,而不是它的定義。此外,避免像#define DLL_EXPORT。預處理器定義應該在項目屬性(MSVC)或命令行選項(例如GCC中的-D)中定義。

看你代碼:

部首

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

當編譯分析此報頭,是認爲DECLDIR作爲dllimport(因爲你定義.cppDLL_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

這應該工作。

+0

Mateusz,謝謝!代碼在您更改後生效。問候波蘭! – 2015-04-06 13:39:37

+0

'__declspec'在定義中是正確的。將宏定義移動到項目屬性是這裏重要的一步。 – 2015-04-06 14:33:38

相關問題