http://www.geeksforgeeks.org/extern-c-in-c/
C + + functions overload (function overloading), but how does the C + + compiler distinguish between different functions? ----is by adding some information in the function name to a different function, the so-called name mangling. The C + + standard does not have a name mangling technology, and each compiler can add different information.
Consider the following function
int F (voidreturn1;} int F (int) return0;} void g (voidint i = f (), j = f (0);}
The C + + compiler may be renamed to (Source:wiki)
int __f_v (voidreturn1;} int __f_i (int) return0;} void __g_v (voidint i = __f_v (), j = __f_i (0);}
How does the C + + linker handle symbols in C language?
The C language does not support function overloading, so there is no name mangling technology, so how do you ensure that C + + linker can get a link to the positive links in C + +?
// Save file as. cpp and use C + + compiler to compile it int printf (constChar *int main () { printf (" Geeksforgeeks " ); return 0 ;}
Compilation Result:
[Email protected]:~/myprog/geeks4geeks/cpp$ g++test11.cpp test11.cpp:1:2: error:invalid preprocessing Directive #int #intprintfConst Char*format,...); ^test11.cpp:In function'int main ()': test11.cpp:5: in: Error:'printf'was not declaredinch ThisScope printf ("geeksforgeeks\n"); ^[email protected]:~/myprog/geeks4geeks/cpp$
The reason for the compilation error is that the C + + compiler has name mangling for the printf function and cannot find the symbol for the renamed function. The workaround is to use the extern "C" keyword.
// Save file as. cpp and use C + + compiler to compile it extern " C " { int printf (constChar *int main () { printf ("geeksforgeeks"); return 0 ;}
Output Result:
[Email protected]:~/myprog/geeks4geeks/cpp$ g++ test11.cpp [email protected]:~/myprog/geeks4geeks/cpp$./A. out Geeksforgeeks
Therefore, all C-language library functions are included in the extern "C" block.
extern " C " {#endif /* */#ifdef __cplusplus}#endif
[C/cpp Series Knowledge] C + + extern "C" name mangling--name mangling and extern "C" in C + +