9

I don't know if I'm missing something obvious, but it appears that I'm unable to compute square roots of a variable in C; the sqrt() function only seems to work on constants. This is my code:

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

int main()
{
    double a = 2.0;
    double b = sqrt(a);
    printf("%f", b);
    return 0;
}

When I run this program, I get the following error:

gcc -Wall -o "test2" "test2.c" (in directory: /home/eddy/Code/euler)
/tmp/ccVfxkNh.o: In function `main':
test2.c:(.text+0x30): undefined reference to `sqrt'
collect2: ld returned 1 exit status
Compilation failed.

However, if I replace the argument in sqrt() with a constant such as 2.0 for example, (b = sqrt(2.0)), then it works fine. Is sqrt() not supposed to work with variables or something?

Thanks for the help

Eddy
  • 6,211
  • 20
  • 56
  • 70

7 Answers7

21

You need to link with the math library (use a '-lm' on the command line). In the constant case, the compiler is probably being smart and precomputing sqrt(2.0) (so the code that is compiled is essentially 'b = 1.414...;')

Gretchen
  • 2,264
  • 17
  • 16
  • That's tedious. What about other libraries, does gcc have one option for each of them? – qed Aug 11 '13 at 17:23
  • @qed no, just one : -l. So, -lm selects a library called 'libm.a'; -lfRod0 would select a library called 'libfRod0.a' In fact it has been thus since ancient times of unix before the dawn of gcc. – greggo Aug 27 '14 at 20:50
3

In case of gcc you need to link the library.

gcc filename.c -lm .

However in case of g++ no need to link the library so this will work fine :

g++ filename.c -o filename Once compilation is successful.

To run simply enter ./filename in G++. and enter ./a.out in Gcc.

2

Use the command gcc -Wall -o "test2" "test2.c" -lm which will likely fix this.

This includes the math library in addition to the standard C runtime library. On most systems, the math library is historically a separate entity that needs to be explicitly requested.

RBerteig
  • 40,004
  • 7
  • 84
  • 125
1

include math library using " " operator

#include " math.h "

compile program using -lm option for inherit math library suppose our program name is test.c the we compile as follow

gcc test.c -lm
antyrat
  • 26,950
  • 9
  • 73
  • 75
hirday
  • 11
  • 1
  • 1
    The `` form in OP is more correct for standard headers. Also, `" math.h "` (with spaces) won't work. – greggo Sep 04 '14 at 20:40
1

gcc does not link the standard libraries by default. So you just need to do this if compiling via gcc:

gcc filename.c -lm .

However in case of g++ no need to link the library so this will work fine :

g++ filename.c -o filename

Abhishek Kaushik
  • 1,083
  • 1
  • 13
  • 17
1

Compile with:

gcc -Wall -o test2 test2.c -lm

You need to link against the math library.

Stephen Canon
  • 100,816
  • 18
  • 175
  • 263
0

This works fine for me. I think there is some problem with ur math library. Try linking it again and see. Other wise code is completely perfect.

EnthuDeveloper
  • 662
  • 9
  • 25