title: gcc 4.2.1 Linking issue: Undefined Symbols for Architecture x86_64
body: <p>Yes, it's been asked before, but every answer I come up with on SO and elsewhere has to do with compiling C++ code in gcc instead of g++, or a issue of some kind with standard libraries. So far, nothing actually lining up right for me here.</p>
  
  <p>So, I'm going through a basic crash course on C, and trying to compile an example used to illustrate linking to files that you create, rather than from the standard libraries. Here's the code:</p>
  
  <p><strong>math_functions.h</strong></p>
  
  <pre><code>int   sum       (int x, int y);
  float average   (float x, float y, float z);
  </code></pre>
  
  <p><strong>math_functions.c</strong></p>
  
  <pre><code>int sum (int x, int y)
  {
    return (x + y);
  }
  
  float average (float x, float y, float z)
  {
    return (x + y + z) / 3;
  }
  </code></pre>
  
  <p>and finally</p>
  
  <p><strong>test3.c</strong></p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "math_functions.h"
  
  main ()
  {
    int   theSum     = sum (8, 12);
    float theAverage = average (16.9, 7.86, 3.4);
  
    printf ("the sum is: %i ", theSum);
    printf ("and the average is: %f \n", theAverage);
    printf ("average casted to an int is: %i \n", (int)theAverage);
  }
  </code></pre>
  
  <p>These are all in the same folder. When I open the Terminal, cd to the folder and run:</p>
  
  <pre><code>gcc test3.c -o test3
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>Undefined symbols for architecture x86_64:
    "_average", referenced from:
      _main in ccmf69Tt.o
    "_sum", referenced from:
      _main in ccxms0fF.o
  ld: symbol(s) not found for architecture x86_64
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>This happens if I use gcc or g++ (which I shouldn't have to do, since this should all be C), and everything I've compiled before now works fine with the</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  </code></pre>
  
  <p>at the top.</p>
  
  <p>I can get it to compile and run fine by removing the</p>
  
  <pre><code>#include "math_functions.h"
  </code></pre>
  
  <p>from the <strong>test3.c</strong> file, putting the contents of <strong>math_functions.h</strong> before main() and the contents of <strong>math_functions.c</strong> after main(). And yes, that is copy and paste, so its definitely the same code.</p>
  
  <p>So yeah, I can get my code to work, but it defeats the purpose of the exercise, in that I don't end up being able to use that code in any other C files without copying it and pasting it into the one file...</p>
  
  <p>So I'm wondering, is there a way I can fix this, so I can include more that just the standard C, C++ and Objective-C libraries?</p>
  
  <p>This happens through box the Terminal, manually typing out the gcc command, and through CodeRunner, which has the standard commands all tucked away into a button, so I can't stuff it.</p>
  
  <p>I'm running Mountain Lion 10.8.4 (12E55) on a 2012 Mac Mini, using the Command Line Tools from Xcode 4.6.2 (installed them just a few hours ago, I haven't actually done much then standard use of the Mini till now)</p>
  
  <p>I have all the same software installed on my MacBook Air, but haven't tested it to see if the same goes down yet.</p>
  
  <p>Any pointers? If someone else has had this and worked it out somewhere here on SO, please point me at it, I have been looking for round an hour but like I said before, all the solutions that I've found so far end up being when there is C++ code or something weird with the standard libraries.</p>