2012-02-03 4 views
0

나는 code :: Blocks로 oop C++에서 작업하고 있습니다.객체 지향 프로그래밍 C++ dll 코드 :: 블록

마이크로 프로세서를 위해 C로 프로그램을 작성하기 때문에 이것들이 내 첫 걸음입니다.

dll을 연결하는 데 문제가 있습니다.

dll을 프로젝트에서 내 주요

입니다 : 당신이 볼 수있는

#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__ 

기본 재료 :

#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 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을 연결하고 사용 할 수 있었다

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

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

와 헤더 :

문제

내가 일 주와 클래스 xclass을 포함하고 있다는 점이다. A는 심지어 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 | undefined xclass::~xclass()'| C:\Users\SoftVision\Desktop\PrinterCode\DLL_test\testDLL\main.cpp|14|undefined reference to에 대한 참조 xclass :: ~ xclass() '| || === 빌드 완료 : 3 개의 오류, 0 경고 === | 도움을

감사합니다 ...

답변

1

사실 당신은 클래스를 내 보내야합니다. 이것은 다른 컴파일러의 다른 RTL 버전 때문에 발생합니다. 대신 클래스의 순수한 가상 인터페이스를 내 보냅니다.

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

또한 피할 매크로 ... 행운을 빕니다 :).

+0

매크로는 무엇입니까? 윈프리? DLL 가드 포함? – harper

0

당신은 너무 클래스를 내 보내야합니다 :

class DLL_EXPORT xclass { 
    //... 

당신은 조금 당신의 헤더를 다시 정렬해야 할 수도 있습니다 - 예를 'main.h'와 'xclass.h'에 모두 포함될 수있는 DLL_EXPORT 어딘가에 #define을 넣으십시오. 당신은 메모리 정렬 몇 가지 문제를 충족 할 수 있기 때문에 순수 가상 클래스가 아닌 클래스를 내보낼 때

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; 
}; 

조심 :

http://www.codeproject.com/Articles/28969/HowTo-Export-C-classes-from-a-DLL

관련 문제