165

I'm very new to C and I have this code:

#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;
}

But when I compile this with:

gcc test.c -o test

I get an error like this:

/tmp/cc58XvyX.o: In function `main':
test.c:(.text+0x2f): undefined reference to `sqrt'
collect2: ld returned 1 exit status

Why does this happen? Is sqrt() not in the math.h header file? I get the same error with cosh and other trigonometric functions. Why?

Wolf
  • 9,679
  • 7
  • 62
  • 108
Ant's
  • 13,545
  • 27
  • 98
  • 148
  • 1
    And here is some speculation why about why `libm` isn't linked by default even though it contains part of the standard library: http://stackoverflow.com/questions/1033898/why-do-you-have-to-link-the-math-library-in-c – Michael Burr May 02 '12 at 07:01
  • It's a duplicate to this one: http://stackoverflow.com/q/5248919/694576 – alk May 02 '12 at 07:10
  • Is there a reason this doesn't throw an error when using an integer literal? Ie. `sqrt(12345)` compiles fine without `-lm`. Is the compiler doing the math? – Brydon Gibson Jul 17 '18 at 17:22
  • it still doesn't work after I follow the accepted answer, what a tired action for a simple function, but call sqrt() with a exact number still work – Phong Nguyen Dec 31 '18 at 09:26

5 Answers5

250

The math library must be linked in when building the executable. How to do this varies by environment, but in Linux/Unix, just add -lm to the command:

gcc test.c -o test -lm

The math library is named libm.so, and the -l command option assumes a lib prefix and .a or .so suffix.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 2
    But this is actually weird that after being a standard library it is not linked automatically by the compiler in Linux. – Ayush Dec 05 '22 at 06:43
  • This comes from many decades ago. In those days, there were widely used options to link alternate libraries. One used the coprocessor. Another implemented the coprocessor calculations in software. I think I remember a joke library which prompted the computer operator "quick: what is 3.46 times 67.884?" – wallyk Dec 05 '22 at 18:39
43

You need to link the with the -lm linker option

You need to compile as

gcc test.c  -o test -lm

gcc (Not g++) historically would not by default include the mathematical functions while linking. It has also been separated from libc onto a separate library libm. To link with these functions you have to advise the linker to include the library -l linker option followed by the library name m thus -lm.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
12

This is a likely a linker error. Add the -lm switch to specify that you want to link against the standard C math library (libm) which has the definition for those functions (the header just has the declaration for them - worth looking up the difference.)

ckhan
  • 4,771
  • 24
  • 26
6

Because you didn't tell the linker about location of math library. Compile with gcc test.c -o test -lm

tuxuday
  • 2,977
  • 17
  • 18
6

Add header:

#include<math.h>

Note: use abs(), sometimes at the time of evaluation sqrt() can take negative values which leave to domain error.

abs()- provides absolute values;

example, abs(-3) =3

Include -lm at the end of your command during compilation time:

gcc <filename.extension> -lm

Akshat
  • 71
  • 1
  • 7