Gcc 4.2.1 Communication Problem: Undefined Symbols for x86_64 Architecture

Yes, it was asked earlier, but every answer that I came up with on SO and elsewhere is related to compiling C ++ code in gcc instead of g ++ or a problem with standard libraries. So far, nothing really suits me here.

So, I am looking at the basic crash course in C and trying to compile an example used to illustrate the link to the files you created, and not from standard libraries. Here is 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); } 

All of them are in one 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 do not need to do, since it should all be C), and everything that I compiled earlier works fine with

 #include <stdio.h> 

up.

I can get it to compile and run fine by deleting

 #include "math_functions.h" 

from test3.c by placing the contents of math_functions.h before main () and the contents of math_functions.c after main (). And yes, it's copying and pasting, so its definitely the same code.

So yes, I can make my code work, but it defeats the goal of the exercise, since I cannot use this code in any other C files without copying it and pasting it into one file ...

So, I am wondering if there is a way to fix this, so I can include more than the standard C, C ++ and Objective-C libraries in it?

This happens through the terminal window, manually entering the gcc command, and through CodeRunner, which has standard commands all hidden in a button, so I can’t write this down.

I am running Mountain Lion 10.8.4 (12E55) on a 2012 Mac Mac using the command line tools from Xcode 4.6.2 (installed them just a few hours ago, I actually have not done much than the standard use of Mini so far)

I have all the same software installed on my MacBook Air, but have not tested it to see if everything is still working.

Any pointers? If someone else did this and worked there somewhere around here, THEN, please indicate to me, I searched all hour, but, as I said, all the solutions that I have found so far are C ++ code or something strange with standard libraries.

+6
source share
1 answer

You just need to compile maths_function. The compiler complains that it does not have the definitions contained in this module.

 gcc test3.c math_functions.c -o test3 
+5
source

Source: https://habr.com/ru/post/946935/


All Articles