title: Using C++ code in a C program and Vice-Versa
body: <p>I'm currently writing an interface between a very low level C program to a higher level  C++ program. The way they relate is through a linked list: The C program has a linked list, and the interface takes the information stored in each node in the linked list and converts that to a C++ vector. The process itself it's not a program. The problem is how to call that C++ function from the C program. Let me give you some light on that:</p>
  
  <pre><code>int importData(List *head, char * source, char * dest);
  </code></pre>
  
  <p>is declared in the C++ file, called <code>import_helper.cpp</code>. I defined the declaration, and shown above, and then the implementation, so the compile wouldn't complain. In <code>import.c</code>, the C program, I'm trying to invoke that function: Remember, List is a struct defined in <code>import.c</code>
  Now, in <code>import.c</code> I have:</p>
  
  <pre><code>#if defined(_cplusplus)
  extern 'C' {
  #endif
  typedef struct list{
     struct list *next
     .. other additional data goes here ...
  }List;
  
  int importData(List *head, char *source, char *dest);
  #if defined(_cplusplus)
  }
  #endif
  </code></pre>
  
  <p>And in <code>import_helper.cpp</code> header I do an <code>#include "import.c"</code>. <code>import.c</code> does not have a .h file (someone wrote that code, I personally consider that a mistake itself).</p>
  
  <p>When I compile, I get:</p>
  
  <pre><code>error: expected unqualified-id before 'class'
  error: conflicts with the new declaration with 'C' linking
  error: previous declaration of 'void getPassword(char *)' with 'C++' linkage 
  </code></pre>
  
  <p>That's just a sample. However, I believe <code>import.c</code> is compiled with <code>gcc</code> and my <code>Build</code> file compiles <code>import_help.cpp</code> with <code>g++</code>. Could that be the reason? I had other files with the similar approach, so I'm not so sure on that. Any Thought?
  Thanks</p>