We all know that export members use _ declspec (dllexport) and import _ declspec (dllimport). You can use these two modifiers to function, class, and variable in the declaration part of the header file, or the class member function is modified to indicate that the Member is an import member or an export member.
One trick is to use macro definition as the switch control so that the DLL project and the project that calls the DLL share a header file:
1. dllCodeDefine a macro in the source file (CPP file) and then include the header file# DefineDllapi _ declspec (dllexport)
# Include"Header. h"
2. Write this in header file header. h.# Ifdef dllapi
# Else
# DefineDllapi _ declspec (dllimport)
# Endif
3. You can use dllapi to modify the exported or imported headers in header. h.VoidDllapi func (Void);
In this way, during macro replacement, the dllapi will be replaced by the _ declspec (dllexport) bits when the DLL code source file is compiled, but the caller has not defined the dllapi, therefore, it will be replaced with _ declspec (dllimport ).