[Notice] I pointed out the undefined "PROJECTNAME_EXPORTS" constant in Microsoft's "Developer Network (Japanese)". Then, even on Sunday morning (2017.10.8), an appropriate reply was posted in less than 40 minutes, and a report to the development team of Microsoft came up with a movie. The excellent engineer is awesome.
Type the project name and click "OK" button.
[Notice] This bug has been fixed in Visual Studio 2017 version 15.4 released on Oct/11/2017.
The project of DLL contains the files "PROJECTNAME.h" and "PROJECTNAME.cpp". I will explain these as "Example.h" and "Example.cpp".
Example.h |
#ifdef EXAMPLE_EXPORTS #define EXAMPLE_API __declspec(dllexport) #else #define EXAMPLE_API __declspec(dllimport) #endif class EXAMPLE_API CExample { public: CExample(void); }; extern EXAMPLE_API int nExample; EXAMPLE_API int fnExample(void); |
Example.cpp (εζηΆζ ) |
#include "stdafx.h" #include "Example.h" EXAMPLE_API int nExample=0; EXAMPLE_API int fnExample(void) { return 42; } CExample::CExample() { return; } |
In case of [1], since Visual Studio defines EXAMPLE_EXPORTS when compiling, EXAMPLE_API in "Example.cpp" is treated as __declspec(dllexport) .
In case of [2], since Visual Studio does not define EXAMPLE_EXPORTS when compiling, EXAMPLE_API in "Example.cpp" is treated as __declspec(dllimport) .
That is, it is a convenient situation that the same "Example.h" can be used in both [1] and [2].
Example.cpp (to avoid bugs) |
#include "stdafx.h" #ifndef EXAMPLE_EXPORTS #define EXAMPLE_EXPORTS #endif #include "Example.h" EXAMPLE_API int nExample=0; EXAMPLE_API int fnExample(void) { return 42; } CExample::CExample() { return; } |