math.h problem -> sqrt undefined

L

LeTubs

Hi

I'm having a bit of problem trying to compile
a program..... which uses math.h

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double sxy, sxx, syyy;
..
..
..
..
r = sxy / sqrt( sxx * syyy );


When I compile I get undefined reference to 'sqrt'
now the funny thing is that I use the function abs
which is the math.h function. I'm sort of at a lose end
does anybody have any ideas ?

Thanks
David
 
J

Jens.Toerring

LeTubs said:
I'm having a bit of problem trying to compile
a program..... which uses math.h
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double sxy, sxx, syyy;
.
r = sxy / sqrt( sxx * syyy );
When I compile I get undefined reference to 'sqrt'
now the funny thing is that I use the function abs
which is the math.h function. I'm sort of at a lose end
does anybody have any ideas ?

Have you read the FAQ? "Undefined reference" is always a linker,
not a compiler problem. You're not linking against the math
library. You probably need to add "-lm" to the command you use
for linking the program.
Regards, Jens
 
E

Eric Amick

When I compile I get undefined reference to 'sqrt'
now the funny thing is that I use the function abs
which is the math.h function. I'm sort of at a lose end
does anybody have any ideas ?

This is in the FAQ, though the FAQ might refer to a different function
in <math.h>. (abs is declared in <stdlib.h>, BTW.)
 
B

bq

LeTubs said:
now the funny thing is that I use the function abs
which is the math.h function.

If you want ablsoute values of floating point numbers use fabs(), not abs().
e.g. abs(5.3) is 5, the integer 5, and when you do
double x;
x = abs(5.3);
that integer get converted to double and you effectively get x = 5.0

bq
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top