[lug] math.h Weirdness

Lori Reed lorireed at lightning-rose.com
Mon Mar 12 20:59:09 MDT 2007

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()



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?

More information about the LUG mailing list