math.sqrt problem in C
Why is the following co开发者_开发问答de throwing me an error undefined reference to sqrt.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
double i = 25;
printf("\nSquare root of %d = %d",i,sqrt(i));
return 0;
}
I have included math.h and I am using Debian.Can anyone tell me what is going wrong .Do i need to install my libraries again or something?
Compile it with the -lm flag on the command line to link to the math library.
The math functions are not linked by default on Linux (since they are comparatively seldom used). You need to add
-lm
to your compiler line to link to libm.so, the math library.
Are you using GCC? Any "undefined reference" means that there is a linking error. In other words, you aren't properly linking to a library that contains the function that it is complaining about. In this case, I believe you can add -lm to your linker flags to link to the math lib.
精彩评论