The following code is throwing undefined symbol error on Linux.
$ cat rms.c
/* sqrt example */
#include <stdio.h>
#include <math.h>
int main ()
{
double param, result;
param = 1024.0;
result = sqrt (param);
printf ("sqrt(%lf) = %lf\n", param, result );
return 0;
}
$ gcc rms.c
/tmp/ccaWecFP.o(.text+0x24): In function `main':
: undefined reference to `sqrt'
collect2: ld returned 1 exit status
If I replace argument to sqrt() with (double)16 then program is compiling and executing. Why is this throwing error in first case.
This is a linker error.
The linker is missing the implementation of sqrt(). It resides in the library libm.
Tell GCC to add it by applying the option -lm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With