Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using math.h sqrt function in C [duplicate]

Tags:

c

unix

math

sqrt

Reading the documentation for math.h, it seems like all I should have to do is include math.h, and use the math functions included, such as sqrt. The problem is I get the following error when trying to use sqrt in my program. I tried math.sqrt, but that did not work, either. Any idea what I am doing wrong?

undefined reference to `sqrt'

...

#include <stdio.h>
#include <math.h>

int main (int argc, char *argv[])
{
  int a, b;

  a = 1;
  b = 5;

  if (a < sqrt (b))
    printf("1 < sqrt(5)");

  return 0;
}
like image 723
Acroyear Avatar asked Sep 20 '26 12:09

Acroyear


1 Answers

You need to explicitely link with the math library as sqrt depends on it. Retry with appending -lm to your compilation line :

gcc you_file.c -o my_sqrt -lm
like image 177
Halim Qarroum Avatar answered Sep 22 '26 01:09

Halim Qarroum