Search code examples
c++dllexport

Using __declspec( dllexport )


Hi I'm little bit confused with dllexport.When I use __declspec( dllexport ) for example in class

 #define DllExport   __declspec( dllexport )  
class DllExport C {  
   int i;  
   virtual int func( void ) { return 1; }  
};  

do I export class C to dll file or do I export C class from dll file?


Solution

  • When compiling the DLL you have to write __declspec(dllexport) as you did. This tells the compiler you want it to be exported. When using the DLL you want __declspec(dllimport) in your included files. The compiler then knows that this functions and classes are in a DLL-file and need to be imported. Because you don't want to change your header-files that much, you should define a macro e.g. BUILD_DLL.

        #ifdef BUILD_DLL
        #define DLL_PORTING __declspec(dllexport)
        #else
        #define DLL_PORTING __declspec(dllimport)
        #endif
    

    Now you write in example.h:

        class DLL_PORTING example_class { … };
    

    In your .exe file just include the header files you need and everything will work.