Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>extern "C" makes a function-name in C++ have 'C' linkage (compiler does not mangle the name) so that client C code can link to (i.e use) your function using a 'C' compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client 'C' linker will then link to using the 'C' name.</p> <p>Since C++ has overloading of function names and C does not, the C++ compiler cannot just use the function name as a unique id to link to, so it mangles the name by adding information about the arguments. A C compiler does not need to mangle the name since you can not overload function names in C. When you state that a function has extern "C" linkage in C++, the C++ compiler does not add argument/parameter type information to the name used for linkage.</p> <p>Just so you know, you can specify "C" linkage to each individual declaration/definition explicitly or use a block to group a sequence of declarations/definitions to have a certain linkage:</p> <pre><code>extern "C" void foo(int); extern "C" { void g(char); int i; } </code></pre> <p>If you care about the technicalities, they are listed in section 7.5 of the C++03 standard, here is a brief summary (with emphasis on extern "C"):</p> <ul> <li>extern "C" is a linkage-specification</li> <li>Every compiler is <em>required</em> to provide "C" linkage</li> <li>a linkage specification shall occur only in namespace scope</li> <li><strike> all function types, function names and variable names have a language linkage </strike> <b><a href="https://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c#comment20842899_1041880">See Richard's Comment:</a></b> Only function names and variable names with external linkage have a language linkage</li> <li>two function types with distinct language linkages are distinct types even if otherwise identical</li> <li>linkage specs nest, inner one determines the final linkage</li> <li>extern "C" is ignored for class members </li> <li>at most one function with a particular name can have "C" linkage (regardless of namespace)</li> <li><strike> extern "C" forces a function to have external linkage (cannot make it static) </strike> <b> See Richard's comment: </b> 'static' inside 'extern "C"' is valid; an entity so declared has internal linkage, and so does not have a language linkage </li> <li>Linkage from C++ to objects defined in other languages and to objects defined in C++ from other languages is implementation-defined and language-dependent. Only where the object layout strategies of two language implementations are similar enough can such linkage be achieved </li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload