title: Is extern "C" only required on the function declaration?
body: <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="https://isocpp.org/wiki/faq/mixing-c-and-cpp#c-calls-cpp" rel="noreferrer">C++ FAQ</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>