Using the flag -lm (add math library)

There is a library of C functions called the "mathematics library" for performing many common math functions. There are functions such as sqrt, pow, sin, cos, sinh, tanh, etc. You can get a listing of the available functions by using the man command's '-k' option:

man -k 3M (or man -k 3m on the Sun workstations)

To learn more about any particular math function, again use the man command to view the individual manual page. For example, to see the manual page for the sqrt function, type this:

man sqrt

You can also type:

man math

for a list of many available functions, as well as details about the mathematics behind them, and information on rounding errors, precisions, and so forth.

When using the math functions in your C program, you need to do two things.

First, put the following line near the top of each C file that will use math functions:

#include <math.h>

This '#include' statement includes the proper declarations for the math library functions.

Second, link with the math library when you are compiling your program. To do this, use the '-lm' option to the C compiler:

cc program.c -lm

Make sure the '-lm' option is the last item on the line. A common mistake is to forget this option when compiling, which results in error messages informing you that the math functions are undefined.