dllimport is a declaration , not a definition . It tells the compiler "trust me, this exists elsewhere." You cannot also define it.
When you build a DLL, you need to tell the compiler which functions, classes, or variables should be accessible to the outside world (the executable or other DLLs). The __declspec(dllexport) directive does exactly that. It instructs the compiler to place the function’s address into the DLL’s export table.
__declspec(dllimport) extern int globalVar; // Declaration only – OK __declspec(dllimport) int globalVar = 42; // ERROR C2491 a function declared dllimport may not be defined
This error rarely occurs because someone writes dllimport above a function definition on purpose. It usually appears due to header file mismanagement or incorrect macro logic.
#ifdef MYLIBRARY_BUILD_DLL #define MYLIBRARY_API __declspec(dllexport) #else #define MYLIBRARY_API __declspec(dllimport) #endif dllimport is a declaration , not a definition
) occurs when a compiler encounters a function body (definition) for a symbol that it has been told is located in an external 1. Root Cause In Windows programming, __declspec(dllimport)
Do not mark inline functions or template instantiations with dllimport / dllexport . Let the linker handle them. The __declspec(dllexport) directive does exactly that
Boom. The error appears.
: The same header is used for both the DLL and the client application, but the logic to switch between is failing. Static Data/Inline Definitions : Attempting to define a data member or an function that is also marked with 3. Technical Solution: The "Switching" Macro