Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqrt() function link error

Tags:

c

sqrt

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.

like image 875
Chandu Avatar asked Jul 31 '26 14:07

Chandu


1 Answers

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.

like image 175
alk Avatar answered Aug 03 '26 02:08

alk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!