Monday, July 13, 2009

extern “C” and __cdecl

In short:
__cdecl is the “C” calling convention whereas
extern “C” is the “C” naming convention.

C as well as C++ code may have __cdecl as the calling convention.

C++ code uses extern “C” to denote that the function is “C” function and the compiler should not mangle the name of the function in the generated object code.

If C++ code uses __cdecl before the function; this means that it follows the “C” calling convention but C++ naming convention; it will have mangled names.

If a C++ code uses both the extern “C” as well as __cdecl it means that the said function uses “C” calling convention as well as the “C” naming convention. The name of the function will be same in the generated object code. There will be no name mangling.

2 comments: