Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Name mangling
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Handling of C symbols when linking from C++==== The job of the common C++ idiom: <syntaxhighlight lang="cpp"> #ifdef __cplusplus extern "C" { #endif /* ... */ #ifdef __cplusplus } #endif </syntaxhighlight> is to ensure that the symbols within are "unmangled" β that the compiler emits a binary file with their names undecorated, as a C compiler would do. As C language definitions are unmangled, the C++ compiler needs to avoid mangling references to these identifiers. For example, the standard strings library, {{code|<string.h>}}, usually contains something resembling: <syntaxhighlight lang="cpp"> #ifdef __cplusplus extern "C" { #endif void *memset (void *, int, size_t); char *strcat (char *, const char *); int strcmp (const char *, const char *); char *strcpy (char *, const char *); #ifdef __cplusplus } #endif </syntaxhighlight> Thus, code such as: <syntaxhighlight lang="cpp"> if (strcmp(argv[1], "-x") == 0) strcpy(a, argv[2]); else memset (a, 0, sizeof(a)); </syntaxhighlight> uses the correct, unmangled {{code|strcmp}} and {{code|memset}}. If the {{code|extern "C"}} had not been used, the (SunPro) C++ compiler would produce code equivalent to: <syntaxhighlight lang="cpp"> if (__1cGstrcmp6Fpkc1_i_(argv[1], "-x") == 0) __1cGstrcpy6Fpcpkc_0_(a, argv[2]); else __1cGmemset6FpviI_0_ (a, 0, sizeof(a)); </syntaxhighlight> Since those symbols do not exist in the C runtime library (''e.g.'' libc), link errors would result. <!-- Useful links. I don't know which should end up in the finished article: * http://www.ucmb.ulb.ac.be/documents/libg++_2.7.1/g++FAQ_35.html * http://theoryx5.uwinnipeg.ca/gnu/gcc/gxxint_15.html This one is ancient, should not be used. * http://www.kegel.com/mangle.html This one describes an old version of Visual C++. * http://www.pgroup.com/ppro_docs/pgiws_ug/pgiug_14.htm -->
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)