How to compile a C program that uses math.h?
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
If you are going to compile a C program with math.h library in LINUX using GCC or G++ you will have to use –lm option after the compile command.
gcc xyz.c -o xyz -lm
Here,
gcc is compiler command (compiler name)
xyz.c is a source file name.
-o is an option to specify the output file.
xyz is the name of the output file.
-lm is an option to link againt the math library (libm).
for more details here is the link containing complete article on it.
Compiling C program with math.h in Linux.