Part of my program is to calculate sqrt
of float number.
When I write sqrt(1.0f);
I success to compile the program,but when I write sqrt(-1.0f);
the compilation fails with undefined reference to 'sqrt'
- I suppose that in this case the nan
value will be returned...
I compile the program uing gcc.
When I compile it with visual studio it is compiled successfuly with negative argument to sqrt.
How the problem could be solved
Thank you
Asked
Active
Viewed 3,260 times
2

Pascal Cuoq
- 79,187
- 7
- 161
- 281

YAKOVM
- 9,805
- 31
- 116
- 217
-
gcc myprog.c -o myprog -lm the -l to include a library m for the math library -lm. – old_timer Jan 08 '12 at 19:41
2 Answers
5
You have to add the -lm
flag on most Unix-based systems, as in:
Compile using:
gcc -c file.c
and then link using:
gcc -o program file.o -lm
Or if you don't want to separate the two compilation steps, simply write:
gcc -o program file.c -lm
4
Link with -lm
to link with the math library

Manuel Selva
- 18,554
- 22
- 89
- 134
-
Can you please give a reason for it. I tried including
in headers, but then facing the error as mentioned in the question. – Akash Chandwani Nov 27 '16 at 18:06