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


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.


Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  เปลี่ยนแปลง )

Google+ photo

You are commenting using your Google+ account. Log Out /  เปลี่ยนแปลง )

Twitter picture

You are commenting using your Twitter account. Log Out /  เปลี่ยนแปลง )

Facebook photo

You are commenting using your Facebook account. Log Out /  เปลี่ยนแปลง )


Connecting to %s