Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to sqrt (or other mathematical functions)

I have this simple code:

max = (int) sqrt (number); 

and in the header I have:

#include <math.h> 

But application still says undefined reference to sqrt. Do you see any problem here? It looks like everything should be okay.

like image 1000
Waypoint Avatar asked Mar 09 '11 16:03

Waypoint


People also ask

How do you solve undefined references to sqrt?

To fix this problem ensure following points:Include header file math. h in your program. Add –lm linker flag with compilation command.

How do you solve an undefined reference to a function in C?

c file. The error: undefined reference to function show() has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show(), i.e., small case names to go further.

How use sqrt function in C Linux?

All you need to do is to use a cast operator to convert the type of double. To work with float numbers simply use the “sqrtf()” function and if you want to deal with long double type then use “sqrtl()”. If you enter any negative value as an input, then the function sqrt() in C shows a domain error.

How do you do square roots in C?

The sqrt() function is defined in math. h header file. To find the square root of int , float or long double data types, you can explicitly convert the type to double using cast operator. int x = 0; double result; result = sqrt(double(x));


2 Answers

You may find that you have to link with the math libraries on whatever system you're using, something like:

gcc -o myprog myprog.c -L/path/to/libs -lm                                        ^^^ - this bit here. 

Including headers lets a compiler know about function declarations but it does not necessarily automatically link to the code required to perform that function.

Failing that, you'll need to show us your code, your compile command and the platform you're running on (operating system, compiler, etc).

The following code compiles and links fine:

#include <math.h> int main (void) {     int max = sqrt (9);     return 0; } 

Just be aware that some compilation systems depend on the order in which libraries are given on the command line. By that, I mean they may process the libraries in sequence and only use them to satisfy unresolved symbols at that point in the sequence.

So, for example, given the commands:

gcc -o plugh plugh.o -lxyzzy gcc -o plugh -lxyzzy plugh.o 

and plugh.o requires something from the xyzzy library, the second may not work as you expect. At the point where you list the library, there are no unresolved symbols to satisfy.

And when the unresolved symbols from plugh.o do appear, it's too late.

like image 99
paxdiablo Avatar answered Sep 30 '22 09:09

paxdiablo


I suppose you have imported math.h with #include <math.h>

So the only other reason I can see is a missing linking information. You must link your code with the -lm option.

If you're simply trying to compile one file with gcc, just add -lm to your command line, otherwise, give some informations about your building process.

like image 26
krtek Avatar answered Sep 30 '22 11:09

krtek