3

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?

Edwin Paco
  • 121
  • 2
  • 7
  • Hopefully the MSDN [dllexport, dllimport](https://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx) information is of some help. – Eljay Feb 25 '18 at 18:51
  • Normally you use a macro that switches between `__declspec( dllexport )` when compiling the dll and `__declspec( dllimport )` when using the dll. Like this: https://stackoverflow.com/questions/14980649/macro-for-dllexport-dllimport-switch – drescherjm Feb 25 '18 at 22:37
  • https://stackoverflow.com/questions/30581837/linker-error-when-calling-a-c-function-from-c-code-in-different-vs2010-project/30583411#30583411 – CristiFati Mar 13 '18 at 19:43

1 Answers1

8

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.

IchiganCS
  • 96
  • 1
  • 6