What does the "DECLDIR __declspec (dllexport)" code really do?
#ifndef _DLL_TUTORIAL_H_ #define _DLL_TUTORIAL_H_ #include <iostream> #if defined DLL_EXPORT #define DECLDIR __declspec(dllexport) #else #define DECLDIR __declspec(dllimport) #endif extern "C" { DECLDIR int Add( int a, int b ); DECLDIR void Function( void ); } #endif What does the DECLDIR __declspec(dllexport) code really do?
In the Microsoft world, __declspec(dllexport) makes a function or class callable from outside the DLL.
When you create a DLL, by default, any functions defined in the DLL can only be called from the same DLL. You cannot call this function from an executable or other DLL.
If you want your function to be called from outside the DLL, you need to export it by adding __declspec(dllexport) .
One way to think about __declspec(dllexport) designate a function as part of a public DLL.
Until you asked about __declspec(dllimport) , it's somehow the other way around. When calling a function in another DLL, your DLL must know that it is part of another public DLL interface so that it can handle the call correctly (calling a function in another DLL requires more complex code that calls the function in itself).
It defines the DECLDIR macro as __declspec(dllexport) . dllexport designed to export functions from a DLL. Here is a quote from this page :
These attributes explicitly define the DLL interface for their client, which may be an executable or another DLL. Declaring functions as dllexport eliminates the need for module-definition (.DEF), at least with respect to the specification of exported functions. Note that dllexport replaces the __export keyword.
If a class is marked declspec (dllexport), any specialization of class templates in the class hierarchy is implicitly marked as declspec (dllexport). This means that the templates are explicit and its members must be defined.
__declspec , by the way, is explained here .