Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to `sqrt' [duplicate]

Tags:

c

nan

sqrt

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

like image 223
Yakov Avatar asked Feb 28 '26 18:02

Yakov


2 Answers

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

Link with -lm to link with the math library

like image 24
Manuel Selva Avatar answered Mar 03 '26 13:03

Manuel Selva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!