You need to guard your __declspec(...)
declaration according to where it is needed.
Create a new header file and name it Exports.h
Put following sample code into it, (you'll understand why you need this later on):
#pragma once
// DLL export macros
#ifdef COMPILE_DLL
#define MY_API __declspec(dllexport)
#elif defined (LINK_DLL)
#define MY_API __declspec(dllimport)
#else // Compile LIB or link LIB
#define MY_API
#endif // COMPILE_DLL
Include that header into every header file in the DLL project that needs to export simbol.
Now to export symbol in the DLL you simply put MY_API
macro in front of a symbol, few examples:
extern "C" MY_API int mysym; // exports mysim without name mangling
MY_API int myint; // exports an int
class MY_API ClassName {}; // exports a class
Next step is to define the macro in project properties in the following location for both the DLL and EXE projects:
Property manager (or right click your project and select properties) -> Common properties -> C\C++ -> Preprocessor
If this is a "DLL" project input following macro:
COMPILE_DLL
If this is an "EXE" project that links the DLL input following macro instead:
LINK_DLL
You need to put appropriate macro for both the DLL and EXE project, then rebuild them.
Notice that if you don't do this then your DLL will be treated as static library, this is intentional because it
allows you build static library by simply not defining anything in preprocessor in project proeprties.
As you can see, symbol is either imported or exported depending on macro definition in properties.
Symbol needs to be exported only when building the DLL, and imported only when linking the DLL.
Note that every DLL project needs to have it's own copy of Exports.h
You need to rename MY_API
according to DLL project name,
For example if your dll project is called MySuperDll then rename macro to something like MY_SUPER_API
,
the whole point is that each DLL project must have it's own API export macro name.
This allows to link multiple DLL's or to link DLL into another DLL optionally in combination with static libraries.