#include <math.h> not working

#include <math.h> not working

Post by Tony Faradjia » Sun, 07 Feb 1999 04:00:00



I've run into a surprising problem with math.h: it doesn't seems to
work.
I'm writing C code to take the square root of a double. I have "#include
<math.h>" at the top, but when I compile (with cc) I get this error
message:

        /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'

What's wrong here?

Thanks,
Tony

 
 
 

#include <math.h> not working

Post by David Z. Maz » Sun, 07 Feb 1999 04:00:00


Tony> I've run into a surprising problem with math.h: it doesn't seems to
Tony> work.
Tony> I'm writing C code to take the square root of a double. I have "#include
Tony> <math.h>" at the top, but when I compile (with cc) I get this error
Tony> message:
Tony>
Tony>        /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'

Okay, this is a linker error, not a compiler error.  This means that,
in fact, the declarations in math.h have been read in (otherwise you'd
be getting a compiler error along the lines of "call to undeclared
function" and the error would be in a .c file), and therefore the
#include works fine.  The linker just doesn't know where to find the
function.  Exercises:

1. Read through math.h.  Pretend you're a compiler/linker.  Ask
   yourself, "I have this declaration that sqrt() and sin() and such
   exist; where's the actual code?"

2. Look for answers to this question on DejaNews or some such service.

3. Read just about any FAQ that deals with using the C language under
   Unix.  This question will be answered.

--

"Hey, Doug, do you mind if I push the Emergency Booth Self-Destruct Button?"
"Oh, sure, Dave, whatever...you _do_ know what that does, right?"

 
 
 

#include <math.h> not working

Post by Tony Faradjia » Sun, 07 Feb 1999 04:00:00


I think I figured out why: I forgot to use the -lm flag with cc.

Thanks for the help, though.
-Tony

 
 
 

#include <math.h> not working

Post by Johan Kjellande » Sun, 07 Feb 1999 04:00:00




> Tony> I've run into a surprising problem with math.h: it doesn't seems to
> Tony> work.
> Tony> I'm writing C code to take the square root of a double. I have "#include
> Tony> <math.h>" at the top, but when I compile (with cc) I get this error
> Tony> message:
> Tony>
> Tony>   /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'

> Okay, this is a linker error, not a compiler error.  This means that,
> in fact, the declarations in math.h have been read in (otherwise you'd
> be getting a compiler error along the lines of "call to undeclared
> function" and the error would be in a .c file), and therefore the
> #include works fine.  The linker just doesn't know where to find the
> function.  Exercises:

> 1. Read through math.h.  Pretend you're a compiler/linker.  Ask
>    yourself, "I have this declaration that sqrt() and sin() and such
>    exist; where's the actual code?"

> 2. Look for answers to this question on DejaNews or some such service.

> 3. Read just about any FAQ that deals with using the C language under
>    Unix.  This question will be answered.

Or add the math library to the linker command. (-lm)
--

Johan Kjellander, Microform AB

http://www.varkon.com (VARKON/English)
http://www.microform.se/toan (Sailing/Swedish)

 
 
 

#include <math.h> not working

Post by M. Buchenried » Sun, 07 Feb 1999 04:00:00



>I've run into a surprising problem with math.h:

No, you didn't.

Quote:>it doesn't seems to
>work.
>I'm writing C code to take the square root of a double. I have "#include
><math.h>" at the top, but when I compile (with cc) I get this error
>message:
>    /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'
>What's wrong here?

No math lib, no math functions.

Michael
--

          Lumber Cartel Unit #456 (TINLC) & Official Netscum
   Note: If you want me to send you email, don't mungle your address.

 
 
 

#include <math.h> not working

Post by Karl Heye » Tue, 09 Feb 1999 04:00:00



> I've run into a surprising problem with math.h: it doesn't seems to
> work.
> I'm writing C code to take the square root of a double. I have "#include
> <math.h>" at the top, but when I compile (with cc) I get this error
> message:

>         /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'

> What's wrong here?

> Thanks,
> Tony

obviously -lm is missing, but I also believe that the ANSI standard
automatically
refers to the maths lib, however you didn't state the -ansi flag.

karl

 
 
 

#include <math.h> not working

Post by Veksler Michae » Tue, 09 Feb 1999 04:00:00


Try linking with -lm (the math library).

   Michael


>  I've run into a surprising problem with math.h: it doesn't seems to
>  work.
>  I'm writing C code to take the square root of a double. I have "#include
>  <math.h>" at the top, but when I compile (with cc) I get this error
>  message:
>    /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'
>  What's wrong here?
>  Thanks,
>  Tony

 
 
 

#include <math.h> not working

Post by D. Stimit » Sat, 13 Feb 1999 04:00:00



> Try linking with -lm (the math library).

>    Michael


> >  I've run into a surprising problem with math.h: it doesn't seems to
> >  work.
> >  I'm writing C code to take the square root of a double. I have "#include
> >  <math.h>" at the top, but when I compile (with cc) I get this error
> >  message:

> >       /tmp/cca021002.o(.text+0x11a): undefined reference to `sqrt'

> >  What's wrong here?

> >  Thanks,
> >  Tony

Keep in mind that any time an undefined symbol shows up, rather than implicit
function declaration warning, you likely don't have the lib linked in. Some
development environments automate lib linking more than others. You can search
for the lib you need to link via:
cd /whateverlibdir
nm libname.so.whatever | grep functionname

The libm.so is correct for your problem, but nm can find the correct lib
even when nobody else knows.