redhat9 gcc sqrt(n)

fredex fredex at fcshome.stoneham.ma.us
Wed Apr 21 23:38:19 UTC 2004


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 at fcshome.stoneham.ma.us -----------------------------
               But God demonstrates his own love for us in this: 
                         While we were still sinners, 
                              Christ died for us.
------------------------------- Romans 5:8 (niv) ------------------------------
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/redhat-list/attachments/20040421/bda88266/attachment.sig>


More information about the redhat-list mailing list