I am very curious about what happens during linking, and, during my research in this area I have stabbed upon this code
#ifdef __cplusplus
extern “C” {
#endif
extern double reciprocal (int i);
#ifdef __cplusplus
}
#endif
The code was in some header file, which was include by .c and .cpp source files of one program. It's a declaration of a function, which is then defined in .cpp file. Why does it work? I mean, during the compilation of the .cpp file this will turn into
extern "C" {
extern double reciprocal (int i);
}
The outer extern both makes the function visible in the global scope and converts the C++ style of function names into C one. But there also is an inner extern. Is it OK the function is externed twice?
#ifdefs and just looking a the second snippet, the former is language linkage, with as you write, the main intent to make the some entities therein (function types, as well as function names and variables with external linkage and variables) to C language linkage, with one main effect that these entities will not have mangled names, and will moreover be placed in global namespace (since there are no namespaces in the C ABI). ...externis not a language linkage specifier but is a storage class specifier, and it will have no effect on the synergy between these two, asreciprocalalready has external linkage even withoutextern(which in turn implies that it has language linkage, making it possibly to link translation units from other languages).