2012-02-03 10 views
0

私はoop C++でcode :: Blocksを使っています。オブジェクト指向プログラミング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

関連する問題