soy muy nuevo en C y tengo este código:¿Por qué aparece el error "referencia no definida a sqrt" aunque incluyo el encabezado math.h?
#include <stdio.h>
#include <math.h>
int main(void)
{
double x = 0.5;
double result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
Pero cuando compilo esto con:
gcc test.c -o test
Puedo obtener un error como este:
/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status
¿Por qué pasó esto? ¿sqrt()
no está en el archivo de encabezado math.h
? Obtengo el mismo error con cosh
y otras funciones trigonométricas. ¿Por qué?
Y aquí hay algunas especulaciones sobre por qué 'libm' no está vinculado por defecto a pesar de que contiene parte de la biblioteca estándar: http://stackoverflow.com/questions/1033898/why-do-you-have- to-link-the-math-library-in-c –
Es un duplicado de este: http://stackoverflow.com/q/5248919/694576 – alk