You seem to be overly obsessed with this "putting around"/"wrapping" thing. At the fundamental level you are not normally supposed to put extern "C"
"around" anything. extern "C"
is a declaration specifier, a linkage specifier, which assigns C-linkage to a specific name. Again, it applies to specific individual names. At the fundamental level you are supposed to declare each of your C++-to-C interface functions in your header file as extern "C"
, as in
extern "C" void foo(void);
extern "C" char bar(int);
Later you can specify the same extern "C"
to the function definitions in the implementation file
extern "C" void foo(void) {
/* whatever */
}
extern "C" char bar(int) {
/* whatever */
}
But strictly speaking, it is not really necessary to repeat extern "C"
in the definition, since if you have already declared your function as extern "C"
, this linkage specification basically gets "inherited" by the definition as well (assuming the declaration is made before the definition in this translation unit). In other words, these function names will not get "mangled", since the compiler knows already, that these names are declared as extern "C"
names.
That's all there's to it. Now, as you already know, you can also use a {}
form of extern "C"
that lets you wrap the entire section of a file into the extern "C"
region. Well, that's just a nice side-feature of extern "C"
that can save you some typing. But normally, you should not just indiscriminately enclose entire files into the extern "C"
region - this is akin to shooting sparrows with a cannon. The specifier is supposed to be applied selectively, only to the names that really need C-linkage. Actually, in many cases you might (and will) have a dedicated C-interface header file that contains only declarations that are supposed to have C-linkage. Putting the extern "C"
around the entire header file in this case is normal practice, but in general case you should use it more carefully and make sure you are not covering with a blanket extern "C"
specifier something that is not supposed to have C-linkage.