I'm taking a programming languages course and we're talking about the extern "C"
declaration.
How does this declaration work at a deeper level other than "it interfaces C and C++"? How does this affect the bindings that take place in the program as well?
extern C affects name mangling by the C++ compiler. Its a way of getting the C++ compiler to not mangle names, or rather to mangle them in the same way that a C compiler would. This is the way it interfaces C and C++.
As an example:
will allow the function to be implemented in a C module, but allow it to be called from a C++ module.
The trouble comes when trying to get a C module to call a C++ function (obviously C can't use C++ classes) defined in a C++ module. The C compiler doesn't like
extern "C"
.So you need to use this:
Now when this appears in a header file, both the C and C++ compilers will be happy with the declaration and it could now be defined in either a C or C++ module, and can be called by both C and C++ code.
extern "C" denotes that the enclosed code uses C-style linking and name mangling. C++ uses a more complex name mangling format. Here's an example:
http://en.wikipedia.org/wiki/Name_mangling
in C:
_example
in C++:
__Z7exampleic
Update: As GManNickG notes in the comments, the pattern of name mangling is compiler dependent.
It should be noted that
extern "C"
also modifies the types of functions. It does not only modify things on lower levels:The type of
&foo
does not equal the type that the typedef designates (although the code is accepted by some, but not all compilers).