Undefined reference to sqrt (or other mathematical functions)

CLinker ErrorsUndefined Reference

C Problem Overview


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.

C Solutions


Solution 1 - C

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.

Solution 2 - C

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.

Solution 3 - C

Just adding the #include <math.h> in c source file and -lm in Makefile at the end will work for me.

	gcc -pthread -o p3 p3.c -lm

Solution 4 - C

Here are my observation, firstly you need to include the header math.h as sqrt() function declared in math.h header file. For e.g

#include <math.h>

secondly, if you read manual page of sqrt you will notice this line Link with -lm.

#include <math.h> /* header file you need to include */

double sqrt(double x); /* prototype of sqrt() function */

Link with -lm. /* Library linking instruction */

> But application still says undefined reference to sqrt. Do you see any > problem here?

Compiler error is correct as you haven't linked your program with library lm & linker is unable to find reference of sqrt(), you need to link it explicitly. For e.g

gcc -Wall -Wextra -Werror -pedantic test.c -lm

Solution 5 - C

I had the same issue, but I simply solved it by adding -lm after the command that runs my code. Example. gcc code.c -lm

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionWaypointView Question on Stackoverflow
Solution 1 - CpaxdiabloView Answer on Stackoverflow
Solution 2 - CkrtekView Answer on Stackoverflow
Solution 3 - CHassan RahmanView Answer on Stackoverflow
Solution 4 - CAchalView Answer on Stackoverflow
Solution 5 - CMohamed MneteView Answer on Stackoverflow