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 the application still says an undefined sqrt link. Do you see any problem here? Everything seems to be fine.

+55
c undefined-reference linker-errors
Mar 09 '11 at 16:32
source share
5 answers

You may find that you need to associate with the math libraries any system you use, for example:

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

Enabling headers allows the compiler to learn about function declarations, but does not necessarily automatically refer to the code needed to execute that function.

Otherwise, you will need to show us your code, the compilation team and the platform on which you work (operating system, compiler, etc.).

The following code compiles and contains links:

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



Just remember that some compilation systems depend on the order in which libraries are listed on the command line. By this, I mean that they can process libraries sequentially and use them only to satisfy unresolved characters at this point in the sequence.

So, for example, taking into account the commands:

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

and plugh.o requires something from the plugh.o library, the second may not work as you expect. The moment you list the library, there are no satisfactory unresolved characters.

And when unresolved characters from plugh.o , it's too late.

+86
Mar 09 2018-11-11T00:
source share

I assume you imported math.h with #include <math.h>

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

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

+24
Mar 09 2018-11-11T00:
source share

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

  gcc -pthread -o p3 p3.c -lm 
+3
Jul 09 '17 at 20:01
source share

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

 #include <math.h> 

secondly, if you read the sqrt manual page, you will notice this line Link to -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 the application still says an undefined reference to sqrt. Do you see the problem here?

The compiler error is correct, since you did not link your program with the lm library & the linker cannot find the link to sqrt() , you need to explicitly link it. For example,

 gcc -Wall -Wextra -Werror -pedantic test.c -lm 
+1
Jul 28 '19 at 5:39 on
source share

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

-one
Feb 05 '17 at 21:32
source share



All Articles