[C/cpp Series Knowledge] C + + extern "C" name mangling--name mangling and extern "C" in C + +

Source: Internet
Author: User
Tags save file

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 + +

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.