title: "Undefined reference to" Error while linking object files
body: <p>I realize this question has been asked in a number of ways including <a href="https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix">this very comprehensive answer</a> but I have looked at many and tried fixing my error to no avail.</p>
  
  <p>I am using .cpp and .c files to create a program. I compiled all files with g++,  it seemed to have no more linking errors, though it gave me a bunch of C++ errors related to the C syntax. This was the command I used:</p>
  
  <pre><code>g++ -o program main.cpp /data/*.c -l[...libs]
  </code></pre>
  
  <p>The main.cpp calls functions in the .c files.
  I then understood that one should not try to compile both .c and .cpp files with one compiler, instead to use gcc for one, g++ for the other, and thereafter to simply link the object files.</p>
  
  <p>So I did this (the .c files are part of a library, and already have .o files)</p>
  
  <pre><code>g++ -c main.cpp
  g++ -o program main.o /data/*.o -l[..libs]
  </code></pre>
  
  <p>But then here I will get "undefined reference to" errors for functions called from main.cpp to the precompiled .c files, errors which I didn't get previously.</p>
  
  <p>Could anyone help? Or do I maybe need to provide more information?</p>
  
  <p>EDIT (a more in depth excerpt of code, I've tried to simplify otherwise this will be impossible to read, but let me know if I still need to add stuff, I'll try to remove unnecessary code):</p>
  
  <p>main.cpp :</p>
  
  <pre><code>#include "header.h"
  
  int main(int argc, char** argv) {
      string s1 = argv[2];
      fn1(s1)
  }
  </code></pre>
  
  <p>header.h</p>
  
  <pre><code>void fn1(string s1)
  </code></pre>
  
  <p>mycfile.c</p>
  
  <pre><code>#include "header.h"
  
  void fn1(string s1){
      fprintf(stdout, " you entered %s", s1);
      fflush(stdout);
  }
  </code></pre>
  
  <p>ANSWER:</p>
  
  <p>@Smeehey helped me figure out the solution, it was actually that I was still including the old headers in an .hpp file I was using. But the core solution was indeed to use the external C{}.</p>