Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc 4.2.1 Linking issue: Undefined Symbols for Architecture x86_64

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.

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:

math_functions.h

int   sum       (int x, int y);
float average   (float x, float y, float z);

math_functions.c

int sum (int x, int y)
{
  return (x + y);
}

float average (float x, float y, float z)
{
  return (x + y + z) / 3;
}

and finally

test3.c

#include <stdio.h>
#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);
}

These are all in the same folder. When I open the Terminal, cd to the folder and run:

gcc test3.c -o test3

I get:

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

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

#include <stdio.h>

at the top.

I can get it to compile and run fine by removing the

#include "math_functions.h"

from the test3.c file, putting the contents of math_functions.h before main() and the contents of math_functions.c after main(). And yes, that is copy and paste, so its definitely the same code.

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...

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?

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.

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)

I have all the same software installed on my MacBook Air, but haven't tested it to see if the same goes down yet.

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.

like image 953
FreelancerJ Avatar asked Jun 10 '13 08:06

FreelancerJ


1 Answers

You just need to compile the maths_function. The linker is complaining it does not have the definitions contained in that module.

gcc test3.c math_functions.c -o test3
like image 111
suspectus Avatar answered Nov 10 '22 17:11

suspectus