title: Can I use shared library created in C++ in a C program?
body: <p>I am creating programs using C. However, I require to use a lot of libraries that have API's only for C++. So, is it possible that I can create a shared object in C++ and then access its functionality using C?</p>
  
  <ol>
  <li>The only data I would be passing and returning would be C compatible data types.</li>
  <li>Converting or migrating to cpp is not an option here.</li>
  </ol>
  
  <p>If it is not possible to interface these codes, how do I get information from C++ code to C code? 
  I tried calling C++ functions from C, but I get errors during linking when I include <code>&lt;string&gt;</code>. So when I call C++ functions from C, should I only use that code which will be C compiler compatible?</p>
  
  <h3>C++ header cppfile.hpp</h3>
  
  <pre><code>#ifndef CPPFILE_H
  #define CPPFILE_H
      #ifdef __cplusplus
      extern "C" {
      #endif
  
      extern int myfunction(const char *filename);
  
     #ifdef __cplusplus
     }
     #endif
  #endif
  </code></pre>
  
  <h3>C++ file cppfile.cpp</h3>
  
  <pre><code>#include "cppfile.hpp"
  #include &lt;string&gt;
  int myfunction(const char *filename) {
      String S(filename);
      return 0;
  }
  </code></pre>
  
  <h3>C file cmain.c</h3>
  
  <pre><code>#include "cppfile.hpp"
  int main(int argc, char **argv)
  {
       int i = myfunction(argv[1]);
       printf("%d\n", i);
       return 0;
  }
  </code></pre>
  
  <h3>Compiling:</h3>
  
  <pre><code>gcc -c cmain.c
  g++ -fPIC -shared -o cppfile.so cppfile.cpp
  </code></pre>