title: Using a C++ Library in a C Project causes a long list of errors
body: <p>I have a <strong>C Project</strong> which uses a <strong>C++ library</strong>. However, if i compile the C Project i get a long list of <em>&quot;undefined reference to XY&quot;</em> errors.</p>
  <p>Those errors are caused by C++ references:</p>
  <p><strong>Errors while compiling:</strong></p>
  <pre><code>In function `&lt;c function&gt;':
  implicit declaration of function `&lt;function&gt;'
  [...]
  </code></pre>
  <p><em>(list shorted)</em><br />
  Where <em>&lt;c function&gt;</em> is the calling function from C Project and <em>&lt;function&gt;</em> the function from the C++ library.</p>
  <p><strong>Errors while linking:</strong></p>
  <pre><code>undefined reference to `std::cerr'
  undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, char const*)'
  undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::operator&lt;&lt; &lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;, std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
  [...]
  undefined reference to `__gxx_personality_v0'
  more undefined references to `__gxx_personality_v0' follow
  </code></pre>
  <p><em>(lots of errors - example shorted)</em></p>
  <p>In the static C++ library my types / functions are enclosed by <code>extern &quot;C&quot;</code> block:</p>
  <pre><code>#ifdef  __cplusplus
  extern &quot;C&quot;
  {
  #endif
  
      void example();
  
  #ifdef  __cplusplus
  }
  #endif
  </code></pre>
  <p>To ensure this, all the <code>#include</code>'s referring to C++ library are surrounded by such a <code>extern &quot;C&quot;</code> block too.</p>
  <p><strong>Compiler:</strong> The <em>C++ library</em> is compiled using <strong>g++</strong>, the <em>C project</em> using <strong>gcc</strong> - both from <strong>MinGW</strong>.</p>
  <p><strong>CL (Linking):</strong> <code>gcc -L&lt;search-path here&gt; -shared [...] -lstdc++ -l&lt;C++ library set here&gt;</code></p>
  <p><em>Please note the <code>-lstdc++</code> flag.</em></p>
  <p><strong>Note:</strong></p>
  <ul>
  <li>The C project contains only <code>.c</code> and <code>.h</code> files – no <code>.cpp</code></li>
  <li>Putting a &quot;dummy&quot; cpp file in like stated <a href="https://stackoverflow.com/questions/376966/using-c-c-static-libraries-from-iphone-objectivec-apps">here</a> didn't work</li>
  <li>MinGW and Eclipse CDT Indigo</li>
  <li>The C++ Library has a name like <code>lib&lt;NAME&gt;.a</code> - it's linked with <code>-l&lt;NAME&gt;</code>, search path is set.</li>
  </ul>
  <hr />
  <hr />
  <h1>Edit:</h1>
  <p>Using <code>g++</code> as linker: <code>undefined reference to </code>std::cerr'` etc. are gone, but i get another list of errors:</p>
  <pre><code>Warning: .drectve `-aligncomm:&quot;___hexdig_D2A&quot;,5' unrecognized
  undefined reference to `&lt;function / class / method from C++ Library&gt;'
  [...]
  undefined reference to `_Unwind_Resume'
  [...]
  undefined reference to `__gxx_personality_v0'
  more undefined references to `__gxx_personality_v0' follow
  undefined reference to `__chkstk_ms'
  [...]
  </code></pre>
  <p><em>(list shorted)</em></p>
  <p>eg. <code>_Unwind_Resume</code> is available in <code>nm</code> of the c++ library.</p>
  <hr />
  <h1>Example:</h1>
  <p>This part is in the Library (C++):</p>
  <p><strong>Test.h</strong> - Header</p>
  <pre><code>#ifndef TEST_H_
  #define TEST_H_
  
  // --- Pure C++ here ---
  #ifdef __cplusplus
  
  #include &lt;string&gt;     /* (1) */
  class Test
  {
  public:
      Test();
      
      
      void aMethod();
      
  private:
      int i;
  };
  #endif
  
  // --- Mixed (C / C++ ) here ---
  
  #ifdef __cplusplus
  extern &quot;C&quot;
  {
  #endif
     
      extern void doTest();
      extern void invoke();
      
  #ifdef  __cplusplus
  }
  #endif
  
  #endif /* TEST_H_ */
  </code></pre>
  <p><strong>Test.cpp</strong> - Implementation of C++ part</p>
  <pre><code>#include &quot;Test.h&quot;
  
  
  Test::Test() : i(0) { }
  
  void Test::aMethod()
  {
      this-&gt;i++;
  }
  
  
  void invoke()
  {
      Test i;
      i.aMethod();
  }
  </code></pre>
  <p><strong>Test.c</strong> - Implementation of C Interface (still in library!)</p>
  <pre><code>#include &quot;Test.h&quot;
  
  void doTest()
  {
      invoke();
  }
  </code></pre>
  <p>Now, here comes the part of the second project (pure C):</p>
  <pre><code>#include &quot;Test.h&quot;
  
  
  int main(int argc, char* argv[])
  {
      doTest();
      
      return 0;
  }
  </code></pre>
  <p><strong><code>(1)</code></strong>: If i remove that include i can compile the C Project. But adding one include for any kind of class or c++ stuff causes lots of errors while linking. unfortunately i need to include classes etc..</p>
  <p>Btw. here's the error i get for this example:</p>
  <pre><code>Test.cpp:(.text+0x1b): undefined reference to `std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string()'
  Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
  Test.cpp:(.text$_ZN4TestD1Ev[Test::~Test()]+0x12): undefined reference to `std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::~basic_string()'
  Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'
  </code></pre>
  <p>setting <code>-lstdc++</code> flag after (!) the flag for the library i get &quot;only&quot; these:</p>
  <pre><code>Test.cpp:(.text+0x64): undefined reference to `_Unwind_Resume'
  Test.cpp:(.eh_frame+0x6b): undefined reference to `__gxx_personality_v0'
  </code></pre>