A Function Declared Dllimport May Not | Be Defined

In this example, the add function is declared with __declspec(dllimport) in mymath.h , but the definition is in mymath.dll . If we don't link against mymath.dll or don't include the necessary header files, the compiler will complain.

__declspec(dllimport) tells the compiler: "This function exists in a different DLL — do not generate code for it here; instead, generate a call via the import library."

Is the function defined as inline ? If so, remove the dllimport .

In your DLL project settings, define a unique name like MYENGINE_EXPORTS . a function declared dllimport may not be defined

__declspec(dllimport) extern int globalVar; // Declaration only – OK __declspec(dllimport) int globalVar = 42; // ERROR C2491

#ifdef MYLIBRARY_BUILD_DLL #define MYLIBRARY_API __declspec(dllexport) #else #define MYLIBRARY_API __declspec(dllimport) #endif

Use a logic block in your header file like this: In this example, the add function is declared

To illustrate the issue, let's consider an example:

Consider this illegal code:

// MyLibrary.def EXPORTS MyFunction

dllimport is a declaration , not a definition . It tells the compiler "trust me, this exists elsewhere." You cannot also define it.

A developer implements a function in an executable project and decides to move it to a DLL. They copy the function declaration into a shared header and add dllimport , but forget to remove the original implementation from the executable’s .cpp file.