views:

99

answers:

3

Hello,

I would like to know what the following declarations do. I have seen them in a C code on MSVisual Studio Compiled code.

extern "C" __declspec(dllexport)

extern "C" __declspec(dllimport)

I know somewhat that they are used to declare external linkage for functions(functional defined in different source file.But would like to know in detail how this works.

-Ajit

+1  A: 

dllexport tells the compiler to generate a .lib file. dllimport tells the compiler to look in a .lib file for the function declaration (its definition will be in a dll).

eduffy
+1  A: 

It means the functions/classes that follow it are visible and accessible across a DLL boundary so you can link against them and call them from other code

Simon
+3  A: 

The extern "C" part tells a C++ compiler that the item being declared should use C linkage, which means that the name will not be mangled (or will be mangled in the same way that a C compiler would). This makes it so the item can be linked to from C code and most other languages as well, since C linkage is typically the standard used for that on a platform.

The __declspec(dllexport) and __declspec(dllimport) items are non-standard attributes that tell the compiler that the item should be exported (or imported) from a DLL. The __declspec() attribute is supported on MS compilers and probably other compilers that target Windows. I'm not sure if GCC does or not. Other storage class attributes that can be specified with __declspec() (at least in MSVC) include uuid(), naked, deprecated and others that provide the compiler with information on how an object or function should be compiled.

Michael Burr