According to (c) ANSI ISO/IEC 14882:2003, page 127:
Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur only in namespace scope (3.3). In a linkage-specification, the specified language linkage applies to the function types of all function declarators, function names, and variable names introduced by the declaration(s).
extern "C" void f1(void(*pf)(int));
// the name f1 and its function type have C language
// linkage; pf is a pointer to a C function
extern "C" typedef void FUNC();
FUNC f2;
// the name f2 has C++ language linkage and the
// function's type has C language linkage
extern "C" FUNC f3;
// the name of function f3 and the function's type
// have C language linkage
void (*pf2)(FUNC*);
// the name of the variable pf2 has C++ linkage and
// the type of pf2 is pointer to C++ function that
// takes one parameter of type pointer to C function
What does all this mean? For example, what linkage does the f2()
function have, C or C++ language linkage?
As pointed out by @Johannes Schaub, there is no real explanation of what this means in the Standard so it can be interpreted differently in different compilers.
Please explain the differences in the object file:
- a function's name with C language linkage and C++ language linkage.
- a function's type with C language linkage and C++ language linkage.
Language linkage is the term used for linkage between
C++
andnon-C++
code fragments. Typically, in a C++ program, all function names, function types and even variable names have the default C++ language linkage.A C++ object code can be linked to another object code which is produced using some other source language (like
C
) using a predefined linkage specifier.As you must be aware of the concept of
name mangling
, which encodes function names, function types and variable names so as to generate a unique name for them. This allows the linker to differentiate between common names (as in the case of function overloading). Name mangling is not desirable when linking C modules with libraries or object files compiled with a C++ compiler. To prevent name mangling for such cases, linkage specifiers are used. In this case,extern "C"
is the linkage specifier. Let's take an example (c++ code mentioned here):Line 1 declares
pfun
to point to a C++ function, because it lacks a linkage specifier.Line 2 therefore declares foo to be a C function that takes a pointer to a C++ function.
Line 5 attempts to call foo with a pointer to g, a C function, a type mis-match.
Diff in function name linkage:
Let's take two different files:
One with
extern "c"
linkage (file1.cpp):One without
extern "c"
linkage (file2.cpp):Now compile these two and check the objdump.
With extern "C" linkage, there is no name mangling for the function
foo
. So any program that is using it (assuming we make a shared lib out of it) can directly call foo (with helper functions likedlsym
anddlopen
) with out considering any name mangling effects.On the other hand, when no
extern "C"
is being used, func:foo
is mangled with some predefined rules (known to compiler/linker being used) and so an application can not directly call it from it specifying the name asfoo
. You can however call it with the mangled name (_Z3fooii
in this case) if you want, but nobody use it for the obvious reason.This page is also a good read for this particular topic.
A nice and clearly explained article about calling convention: http://www.codeproject.com/KB/cpp/calling_conventions_demystified.aspx