main.c.text+0x35): undefined reference to `sqrt'
collect2: ld returned 1 exit status
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 10;
double y;
y = sqrt(x);
return 0;
}
I've checked that the math.h file is indeed in the following folder:
/usr/include
But why can't gcc find it? How can I use math.h?
Thanks for any help!
including math.h is not the problem. gcc is just missing the code for the
math functions. You can achieve this by adding "-lm" to your gcc command
line. Now, the math library is added.
gcc main.c -lm
hth
Guenther
the header files only have the declarations of the functions, not the source
itself. The functions in stdio.h are in the glibc library, which is linked
by default. The math functions are not defined in glibc, they are in the
math library. And to link this library you have to "-lm".
hth
Guenther
The compiler automatically links against libc (the C library) so that
you don't have to specify "-lc" every time. The math functions are in
libm though, and since the compiler doesn't link against it
automatically, you need "-lm".