Why am I getting "undefined reference to sqrt" error even though I include math.h header? [duplicate]
Asked Answered
B

5

174

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?

Betthezul answered 2/5, 2012 at 6:53 Comment(4)
And here is some speculation why about why libm isn't linked by default even though it contains part of the standard library: #1034398Charpentier
It's a duplicate to this one: https://mcmap.net/q/144593/-undefined-reference-to-sqrt-or-other-mathematical-functions/694576Zima
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?Wagram
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 workJann
A
257

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.

Anselme answered 2/5, 2012 at 6:55 Comment(3)
But this is actually weird that after being a standard library it is not linked automatically by the compiler in Linux.Astylar
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?"Anselme
It's worth specifying that -lm must be after the source file, otherwise the error is produced anyway.Saltation
M
44

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. Observe that the order of arguments matter, it does not work if -lm is placed immediately after gcc.

Microspore answered 2/5, 2012 at 6:55 Comment(0)
D
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.)

Doering answered 2/5, 2012 at 6:55 Comment(0)
S
6

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

Sector answered 2/5, 2012 at 6:56 Comment(0)
C
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

Casimir answered 3/3, 2017 at 5:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.