Saturday, January 28, 2006

gcc + math.h

On Wed, Apr 21, 2004 at 04:24:48PM -0600, Nick Olivas wrote:
> When I compile with gcc and use the function sqrt(n) it returns
> .
>
> When I compile the same code with g++ compilation is successful - no
> warnings, no errors.
>
> I assume that both gcc and g++ use the same file?
>
> Any suggestions

yes:

gcc -o foo foo.c -lm

put the "-lm" at the end of the command line.

The "math" functions (sqrt, pow, trig stuff, and similar) are all
in a separate library, libm.a. Why? For "historical reason". It's just
always been that way on Unix. There was probably a good reason back in
the deep dark mists of time, but now it's mostly tradition.

Since it's a LIBRARY problem, not a HEADER problem, that just points up
the likelihod that c++ programs link with a different (set of) library(ies)
than do C programs, and that the C++ libs include the math stuff while
the C libs require an extra lib to resolve those symbols.

No comments:

Post a Comment