[ back to toc ]

compiling c program

Date: 2002/01/16 10:52


When i use math.h in a c program.....why do i need to use -lm when
compiling the program....can u give a full definition of this -lm
When you use cc or gcc you actually start a program that starts many other
programs one after the other. First it starts cpp, which is the C
preprocessor. Then it starts the C compiler and finally the linker. The C
preprocessor creates a text file from your source that contains the pure C
code with the macros and include file put into evaluated. The C compiler
creates an object file that contains your code. This is the code that you
coded in C, but it does not contain code for functions like printf, sin or
cos. The code for these functions are contained in external files that
were already compiled when Linux was built. These library files are linked
to the object file that you have created and thus you get the final

When you do not use mathematical functions the standard library containing
for example printf is automatically linked to your object code. This is
because all programs use these functions. On the other hand not all
programs use mathematical functions. Therefore the math library has to be
specified to be used and linked to your object code. That is what -lm
stands for. Actually if you specify -lXXX then the the library libXXX.a or
libXXX.so will be used to link to your object file. The files are in /lib
and /usr/lib directories.


[ back to toc ]