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 > <undefined reference to 'sqrt'>. > > 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 <math.h> 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. -- ---- Fred Smith -- fredex@xxxxxxxxxxxxxxxxxxxxxx ----------------------------- But God demonstrates his own love for us in this: While we were still sinners, Christ died for us. ------------------------------- Romans 5:8 (niv) ------------------------------
Attachment:
pgp00852.pgp
Description: PGP signature
-- redhat-list mailing list unsubscribe mailto:redhat-list-request@xxxxxxxxxx?subject=unsubscribe https://www.redhat.com/mailman/listinfo/redhat-list