[lug] math.h Weirdness

Bill Thoen bthoen at gisnet.com
Tue Mar 13 07:09:50 MDT 2007


Thanks Lori,

The sqrt example listed in that article was exactly the situation I
encountered. Using -lm did the trick. Funny thing though; with some code 
I write I don't need to explicitly link to the math library, but on this
module, I had to or it wouldn't resolve sqrt(). 

Also, I found that the protoype for sqrt _is_ in math.h. It's immplemented
as a macro in bits/mathcalls.h and included.

Thanks for the help!

- Bill


On Mon, Mar 12, 2007 at 08:59:09PM -0600, Lori Reed wrote:
> The sqrt() should certainly be prototyped by including math.h, but many 
> header files include other header files, so it may actually be 
> prototyped elsewhere.
> 
> However, the linker doesn't care about header files, so the problem is 
> you're not linking in the appropriate library. Try adding -lm to the 
> link command.
> 
> This page directly addresses linking sqrt()
> 
> http://www.network-theory.co.uk/docs/gccintro/gccintro_17.html
> 
> Lori
> 
> Bill Thoen wrote:
> 
> >Am I mistaken in thinking that in C the sqrt() function is prototyped in
> >math.h? I just tried to build a simple C program that called sqrt() and the
> >linker balked. I then tried:
> >
> >$ grep sqrt /usr/include/math.h
> >define M_2_SQRTPI     1.12837916709551257390  /* 2/sqrt(pi) */
> >define M_SQRT2        1.41421356237309504880  /* sqrt(2) */
> >define M_SQRT1_2      0.70710678118654752440  /* 1/sqrt(2) */
> >define M_2_SQRTPIl    1.1283791670955125738961589031215452L  /* 2/sqrt(pi) 
> >*/
> >define M_SQRT2l       1.4142135623730950488016887242096981L  /* sqrt(2) */
> >define M_SQRT1_2l     0.7071067811865475244008443621048490L  /* 1/sqrt(2) 
> >*/
> >
> >It's not there! Shouldn't it be? If not, where is it these days, or has the
> >square root been retired?
> 
> _______________________________________________
> Web Page:  http://lug.boulder.co.us
> Mailing List: http://lists.lug.boulder.co.us/mailman/listinfo/lug
> Join us on IRC: lug.boulder.co.us port=6667 channel=#colug
> 



More information about the LUG mailing list