GCC math functions not found

GCC math functions not found

Post by Rich Dubraws » Thu, 27 Mar 1997 04:00:00



I am finally starting to write code in linux after a couple of
years of DOS/WIndows programming and I immediately stumbled
across a porblem.  
I am using version 2.7.2.f.1 of gcc ( it has the g77 fortran
compiler in it) and although I can write a program that used
stdio.h and malloc.h and they compile and work nicely, when
I tried to use a math.h function ( in this case sin, cos and tan)
I get the message that there is an undefined reference to (sin
 cos or tan (3 different lines).  I would really appreciate any
light that could be shed on this.  I am using what originally was
a Slackware distribution if that helps, I am compiling the code
as:  gcc -ggdb -o simple_math simple_math.c
Thanks in advance.
Rich Dubrawski

 
 
 

GCC math functions not found

Post by Dominik Auf der Mau » Thu, 27 Mar 1997 04:00:00



> ...
> I tried to use a math.h function ( in this case sin, cos and tan)
> I get the message that there is an undefined reference to (sin
>  cos or tan (3 different lines).  I would really appreciate any

Stupid question: Do you link it with the '-lm' (link with math library)
option?

- Dominik

 
 
 

GCC math functions not found

Post by David Sye » Thu, 27 Mar 1997 04:00:00



> as:  gcc -ggdb -o simple_math simple_math.c

add -lm to the end of that line

Dave.

 
 
 

GCC math functions not found

Post by Peter Wieneman » Mon, 31 Mar 1997 04:00:00




> > ...
> > I tried to use a math.h function ( in this case sin, cos and tan)
> > I get the message that there is an undefined reference to (sin
> >  cos or tan (3 different lines).  I would really appreciate any
> Stupid question: Do you link it with the '-lm' (link with math library)
> option?

Mind that questions are never stupid! Only answers can be stupid.

Someone who asks questions may be a fool; for a few minutes.
Those who do not ask are fools for the whole life.

--
   _/_/_/ _/     _/  Peter Wienemann    email:


_/     _/_/ _/_/     Germany

 
 
 

GCC math functions not found

Post by fae240 » Mon, 07 Apr 1997 05:00:00



> I am finally starting to write code in linux after a couple of
> years of DOS/WIndows programming and I immediately stumbled
> across a porblem.  
> I am using version 2.7.2.f.1 of gcc ( it has the g77 fortran
> compiler in it) and although I can write a program that used
> stdio.h and malloc.h and they compile and work nicely, when
> I tried to use a math.h function ( in this case sin, cos and tan)
> I get the message that there is an undefined reference to (sin
>  cos or tan (3 different lines).  I would really appreciate any
> light that could be shed on this.  I am using what originally was
> a Slackware distribution if that helps, I am compiling the code
> as:  gcc -ggdb -o simple_math simple_math.c
> Thanks in advance.
> Rich Dubrawski


Rich,
        This is a common problem with new unix hackers...
when using mah libraries (and many, many other libraries) you have to
tell the linker to pull them in.
You probably got errors telling you you have declared 56 or so math
functions and the one you are using isn't defined.  The reason is that the
header file is being pulled in and declaring evry function in math.h (as
usual) but when the linker gets called it doesn't know where the functin
definitions are.
In general try calling gcc like this:
gcc -g -o {etc.} filename -l(whatever library)
example for math as that's what yor problem was with:
gcc -g myhack.c -lm
for Rogue Libraries it would look like this:
gcc -g whatever.c -lrwtool
and if you used them together you'd add -lm like:
gcc -g myfile.c -lrwtool -lm

ok?

 btw: (malloc.h, stdlib.h, stdio.h, etc - the standard libraries for
basic io, etc are already nown to the compiler which is why you do not
have to specify for them)

Frank