Ubuntu – How to compile a C program that uses math.h


I am having problems in math.h header file and when I use square root function as in sqrt(d). But my compiler is not supporting this. Please advise me about this problem.

My Ubuntu version is 2012.

Best Answer

Append -lm to the end of your gcc command.

With all recent versions of GCC on GNU/Linux systems like Ubuntu, when you use the math library, you have to explicitly link to it. It is not automatically linked to along with the rest of the standard C library.

If you are compiling on the command-line with the gcc or g++ command, you would accomplish this by putting -lm at the end of the command.

For example: gcc -o foo foo.c -lm