Note that there are some explanatory texts on larger screens.

plurals
  1. POIs extern "C" only required on the function declaration?
    primarykey
    data
    text
    <p>I wrote a C++ function that I need to call from a C program. To make it callable from C, I specified <code>extern "C"</code> on the function <strong>declaration</strong>. I then compiled the C++ code, but the compiler (Dignus Systems/C++) generated a <a href="http://en.wikipedia.org/wiki/Name_mangling" rel="noreferrer">mangled name</a> for the function. So, it apparently did not honor the <code>extern "C"</code>. </p> <p>To resolve this, I added <code>extern "C"</code> to the function <strong>definition</strong>. After this, the compiler generated a function name that is callable from C.</p> <p>Technically, the <code>extern "C"</code> only needs to be specified on the function declaration. Is this right? (The <a href="http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.6" rel="noreferrer">C++ FAQ Lite</a> has a good example of this.) Should you also specify it on the function definition? </p> <p>Here's an example to demonstrate this:</p> <pre><code>/* ---------- */ /* "foo.h" */ /* ---------- */ #ifdef __cplusplus extern "C" { #endif /* Function declaration */ void foo(int); #ifdef __cplusplus } #endif /* ---------- */ /* "foo.cpp" */ /* ---------- */ #include "foo.h" /* Function definition */ extern "C" // &lt;---- Is this needed? void foo(int i) { // do something... } </code></pre> <p><em>My issue may be the result of incorrectly coding something, or I may have found a compiler bug. In any case, I wanted to consult stackoverflow to make sure I know which is technically the "right" way.</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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