2012-02-03 15 views
0

我正在使用代碼::塊的oop C++。面向對象編程C++ dll代碼::塊

這些都是我在接力的第一個步驟,因爲我在C程序的微處理器。

我無法鏈接一個DLL。

我從DLL項目主要是:

#include "main.h" 
#include "xclass.h" 

// a sample exported function 
void DLL_EXPORT SomeFunction(const LPCSTR sometext) 
{ 
    MessageBoxA(0, sometext, "DLL Message", MB_OK | MB_ICONINFORMATION); 
} 

BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 
{ 
    switch (fdwReason) 
    { 
     case DLL_PROCESS_ATTACH: 
      // attach to process 
      // return FALSE to fail DLL load 
      break; 

     case DLL_PROCESS_DETACH: 
      // detach from process 
      break; 

     case DLL_THREAD_ATTACH: 
      // attach to thread 
      break; 

     case DLL_THREAD_DETACH: 
      // detach from thread 
      break; 
    } 
    return TRUE; // succesful 
} 

這是標題:

#ifndef __MAIN_H__ 
#define __MAIN_H__ 

#include <windows.h> 
#include "xclass.h" 

/* To use this exported function of dll, include this header 
* in your project. 
*/ 
#ifdef BUILD_DLL 
    #define DLL_EXPORT __declspec(dllexport) 
#else 
    #define DLL_EXPORT __declspec(dllimport) 
#endif 


#ifdef __cplusplus 
extern "C" 
{ 
#endif 

void DLL_EXPORT SomeFunction(const LPCSTR sometext); 

#ifdef __cplusplus 
} 
#endif 

#endif // __MAIN_H__ 

,你可以看到基本的東西。

的問題是,我包括類xclass與日主:

#include "xclass.h" 
xclass::xclass() 
{ 
    //ctor 
} 

xclass::~xclass() 
{ 
    //dtor 
} 

和頭

#ifndef XCLASS_H 
#define XCLASS_H 

class xclass 
{ 
    public: 
     xclass(); 
     virtual ~xclass(); 
     unsigned int GetCounter() { return m_Counter; } 
     void SetCounter(unsigned int val) { m_Counter = val; } 
    protected: 
    private: 
     unsigned int m_Counter; 
}; 

#endif // XCLASS_H 

我能夠在其他項目的鏈接,並使用DLL。一個甚至可以使用該功能在DLL SomeFunction("teste x");但我不能訪問和我們的類:

#include <iostream> 
#include "main.h" 
//#include "../cvWrapper/main.h" 

using namespace std; 

int main() 
{ 
    xclass ClassInDll;// not working 

    SomeFunction("teste x"); //using the function in dll 

    printf("%d\n", 1); 
    return 0; 
} 

生成錯誤是:

|| === testDLL,調試=== | obj \ Debug \ main.o ||在函數main':| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|9|undefined reference to xclass :: xclass()'| C:\ Users \ SoftVision \ Desktop \ PrinterCode \ DLL_test \ testDLL \ main.cpp | 14 | undefined 參考xclass::~xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference to xclass ::〜xclass()'| || ===構建完成:3個錯誤,0 警告=== |

感謝您的幫助...

回答

1

其實你應該導出類:

class DLL_EXPORT xclass 
{ 
    public: 
     xclass(); 
     virtual ~xclass(); 
     unsigned int GetCounter() { return m_Counter; } 
     void SetCounter(unsigned int val) { m_Counter = val; } 
    protected: 
    private: 
     unsigned int m_Counter; 
}; 

,在導出類,這是不是一個純虛類,因爲你可能會遇到一些問題與內存對齊要小心。發生這種情況是因爲不同編譯器中的RTL版本不同。而是導出你的類的純虛擬接口。

class DLL_EXPORT IXClass 
{ 
    public: 
     IXClass(); 
     virtual ~IXClass(); 
     virtual unsigned int GetCounter()=0; 
     virtual void SetCounter(unsigned int val) =0; 
}; 

也避免宏... 祝你好運:)。

+0

什麼宏? WINAPI? DLL包含衛兵? – harper 2013-09-29 12:00:11