Name Mangling and extern “C” in C++

14 Mar, 2022
C++ supports function overloading, i.e., there can be more than one function with the same name but, different parameters. How does the C++ compiler distinguish between different functions when it generates object code – it changes names by adding information about arguments. This technique of adding additional information to function names is called Name Mangling. C++ standard doesn’t specify any particular technique for name mangling, so different compilers may append different information to function names. 
Consider the following example of Name Mangling, having the various declarations of function f(): 
CPP
// Function overloading in CPP to demonstrate// Name Mangling int f(void) { return 1; } int f(int) { return 0; } void g(void) { int i = f(), j = f(0); }
Some C++ compilers may mangle the above names to the following,  
CPP
// Function overloading to demonstrate// Name Mangling int __f_v(void) { return 1; } int __f_i(int) { return 0; } void __g_v(void) { int i = __f_v(), j = __f_i(0); }
Note: C does not support function overloading, So, when we link a C code in C++, we have to make sure that name of a symbol is not changed.
In C, names may not be mangled as it doesn’t support function overloading. So how to make sure that name of a symbol is not changed when we link a C code in C++. For example, see the following C++ program that uses printf() function of C.  
C++
// C Program to demonstrate it// doesn't support Name Mangling int printf(const char* format, ...); // Driver Codeint main(){    printf("GeeksforGeeks");    return 0;}
The above program generates an error.
Compiler Error:
In function `main’:f84cc4ebaf5b87bb8c6b97bc54245486.cpp:(.text+0xf): undefined reference to `printf(char const*, ...)’collect2: error: ld returned 1 exit status
Explanation: The reason for compiler error is simple, the name of printf() is changed by the C++ compiler and it doesn’t find the definition of the function with a new name.
Solution: Extern “C” in C++
When some code is put in the extern “C” block, the C++ compiler ensures that the function names are un-mangled – that the compiler emits a binary file with their names unchanged, as a C compiler would do.If we change the above program to the following, the program works fine and prints “GeeksforGeeks” on the console(as shown below). 
C++14
// CPP Program to demonstrate Extern "C" extern "C" {int printf(const char* format, ...);} // Driver Codeint main(){    printf("GeeksforGeeks");    return 0;}
GeeksforGeeks
Therefore, all C style header files (stdio.h, string.h, etc) have their declarations in the extern “C” block.
CPP
#ifdef __cplusplusextern "C" {#endif// Declarations of this file#ifdef __cplusplus}#endif
Following are the main points discussed above: 1. Since C++ supports function overloading, additional information has to be added to function names (called Name mangling) to avoid conflicts in binary code. 2. Function names may not be changed in C as it doesn’t support function overloading. To avoid linking problems, C++ supports the extern “C” block. C++ compiler makes sure that names inside the extern “C” block are not changed.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
23603vaibhav2021
anshikajain26
RishabhPrabhu
Extern "C"
C Language
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.