goldenmean
goldenmean

Reputation: 18966

What is the explanation behind the below declaration/keyword?

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

Upvotes: 0

Views: 396

Answers (3)

Michael Burr
Michael Burr

Reputation: 340218

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.

Upvotes: 3

Simon
Simon

Reputation: 80769

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

Upvotes: 1

eduffy
eduffy

Reputation: 40224

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).

Upvotes: 1

Related Questions