Question:
GCC linker problem?
colinmeister
2006-10-14 05:57:37 UTC
Trying to compile a fairly simple c program on my Sun Sparc running Solaris 9 O/S. I have got rid of all compiler errors and warnings, but get the following message:

# gcc BTDC.c
Undefinedfirst referenced
symbol in file
cos /var/tmp/cczK10be.o
sin /var/tmp/cczK10be.o
sqrt /var/tmp/cczK10be.o
typeq /var/tmp/cczK10be.o
degcalc /var/tmp/cczK10be.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status

I am obviously writing #include in my program, but it seems the linker is not finding the math functions.

Thanks for any help.
Four answers:
Dan M
2006-10-14 14:31:45 UTC
Just add -lm to the end of the linker command line, the math library is optional.



Note that just including just includes the headers, the linker still needs a reference to libm in order to be able to resolve the addresses of those functions.



This is all extensively documented.



Regards, Dan.
anonymous
2006-10-14 07:29:54 UTC
In C, you may have a different bug in the compiler directories. You may need to specifically find the math library file and check that the directory it is in is on the path of the OS and the compiler. It seems from the error message that it is likely the header file isn't being found, but this error also could be a result of not finding the actual object code in the library file or not finding the library file itself. You may also want to try using #include "./math.h" or some similar specifier for a relative path. This may allow you to find the problem a bit quicker than you would poking about in the directories and paths alone. I hope this helps solve the problem.
Mike
2006-10-14 06:10:59 UTC
As you have guessed the maths library is not being linked.



I think the command you are missing is either -lm or -dld , I forget which. Add those you your linking command line



gcc BTDC.o -o prog1 -lm
anonymous
2006-10-14 06:07:15 UTC
Manually locate the math.h file in the C++ library directory



If its not there then download it:



http://mirror.sg.depaul.edu/pub/OpenBSD/src/include/math.h



Check they are the same if it is there.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...