title: compiling c and c++ files using g++
body: <p>I am trying to compile a C and C++ file and link them together. I am following the answers from this link - <a href="https://stackoverflow.com/questions/5430688/compiling-c-and-c-files-together-using-gcc">Compiling C and C++ files together using GCC</a></p>
  
  <p>However I have a different problem, which is not explained in that post. I have defined my main() in the C++ file and using a function whose details are there in the C file. The declaration of the function is there in a .h file, which is included both in the C and C++ file.</p>
  
  <p>My C++ file - </p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;testc.h&gt;
  
  using namespace std;
  
  extern "C" void cfunc(int, int);
  
  int main()
  {
      cout&lt;&lt;"Hello from cpp"&lt;&lt;endl;
      cfunc(3,6);
  }
  </code></pre>
  
  <p>My C file -</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;testc.h&gt;
  
  int cfunc2(int a, int b)
  {
      return a+b;
  }
  
  void cfunc(int a, int b)
  {
      printf("Hello from c %d\n",cfunc2(a,b));
  }
  </code></pre>
  
  <p>My .h file -</p>
  
  <pre><code>int cfunc2(int, int);
  void cfunc(int, int);
  </code></pre>
  
  <p>As per other posts if I use a C function in my C++ code, I need to give the following definition in my C++ file -</p>
  
  <pre><code>extern "C" void cfunc(int, int);
  </code></pre>
  
  <p>However when I run like this I get the following error -</p>
  
  <pre><code>testcpp.cpp:6:17:  error: conflicting declaration of ‘void cfunc(int, int)’ with ‘C’ linkage
  extern "C" void cfunc(int, int);
  
  In file included from testcpp.cpp:2:0:
  inc/testc.h:9:6: note: previous declaration with ‘C++’ linkage
  void cfunc(int, int);
  </code></pre>
  
  <p>testcpp.cpp is where I make the call from main, testc.c contains the function definition and testc.h is the header file.</p>
  
  <p>I run the following set of commands -</p>
  
  <pre><code>gcc -c -std=c99 -o testc.o testc.c -Iinc
  g++ -c -std=c++0x -o testcpp.o testcpp.cpp -Iinc
  g++ -o myapp testc.o testcpp.o
  </code></pre>
  
  <p>The inc folder contains the .h file</p>
  
  <p>Anything I am doing wrong?</p>