Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using __declspec( dllexport )

Tags:

c++

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?

like image 880
Edwin Paco Avatar asked Sep 04 '26 07:09

Edwin Paco


1 Answers

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.

like image 120
IchiganCS Avatar answered Sep 06 '26 21:09

IchiganCS