Name mangling and extern "C" in C + +

Source: Internet
Author: User
Tags save file

Original link: http://www.geeksforgeeks.org/extern-c-in-c/

C + + supports function overloading, i.e., there can be more than one functions with same name and differences in parameters . How does C + + compiler distinguishes 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 no particular technique for name mangling, so different compilers may append different infor Mation to function names.

Consider following declarations of function f ()

1 int  F (voidreturn1;} 2 int  F (int)  return0;} 3 void g (voidint i = f (), j = f (0);}

A C + + compiler may mangle above names to following (Source:wiki)

1 int  __f_v (voidreturn1;} 2 int  __f_i (int)  return0;} 3 void __g_v (voidint i = __f_v (), j = __f_i (0);}


How to handle C symbols when linking from C + +?
In C, names is mangled as C doesn ' t support function overloading. So-to-make sure, that's name of a symbol is not changed if we link a C code in C + +. For example, see the following C + + program, uses printf () function of C.

1 //Save file as. cpp and use C + + compiler to compile it2 intprintfConst Char*format,...);3 4 intMain ()5 {6printf"Geeksforgeeks");7     return 0;8}

Output:

 1 undefined reference to ' printf (charconst*, ...) '  1 exit status

The reason for compiler error was simple, name of printf is changed by C + + compiler and it doesn ' t find definition Of the function with new name.

The solution of problem is extern "C" in C + +. When some code was put in extern "C" block, the C + + compiler ensures that the function names was unmangled–that the Compi Ler emits a binary file with their names unchanged, as a C compiler would do.

If We change the above program to following, the program works fine and prints ' geeksforgeeks ' on console.

1 //Save file as. cpp and use C + + compiler to compile it2 extern "C"3 {4     intprintfConst Char*format,...);5 }6 7 intMain ()8 {9printf"Geeksforgeeks");Ten     return 0; One}

Output:

 1 geeksforgeeks

Therefore, all C style header files (stdio.h, String.h,.. etc) has their declarations in extern "C" block.

12extern"C"  {3 #endif  4/     **/5#ifdef __cplusplus6}  7#endif

Following is main points discussed above
1. Since C + + supports function overloading, additional information have to is added to function names (called Name mangling) To avoid conflicts in binary code.
2. function names May is changed in C as C doesn ' t support Function overloading. To avoid linking problems, C + + supports extern "C" block. C + + compiler makes sure that names inside extern "C" block is not changed.

Name mangling and extern "C" in C + +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.