June 25th, 2012, 01:56 PM
Issue with <math.h> and sqrt() [C]
My compiler isn't recognizing sqrt() despite including <math.h>. Help? The compiler error is "main.c:16: undefined reference to `sqrt'"
int a, b, c;
float x1, x2;
x1 = (-b + sqrt((b*b)-(4*a*c)))/(2*a);
x2 = (-b - sqrt((b*b)-(4*a*c)))/(2*a);
printf("x1 = %.3f", x1);
printf("x2 = %.3f", x2);
June 25th, 2012, 04:47 PM
If your using GCC, you need to compile it with the -lm flag in order to include the math library which is where the functions from math.h are defined.
Recycle your old CD's, don't just trash them
If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud