C libraries on UNIX/LINUX


When including math.h in a program, you must add the -lm option to the end of the the gcc command line to force the linker to include the math library.  Otherwise, the program will not compile.C libraries are either static or dynamic, depending on how they are linked with an application program during compilation.  Static library routines are copied into the executable file by the linker at compile time.  Dynamic library routines are not combined with the program at compile time; instead, they are loaded into memory at run time when first called by the executing program.

By default, the compiler uses the dynamic version of a library whenever possible.  The compiler can be forced to use static libraries via the use of the compiler flag -static.  The static version of the C math library is called libm.a (.a is for "archive" in UNIX jargon).  The corresponding dynamic version of the C math library is called libm.so (.so is for "shared object" in UNIX jargon).

You can print the libraries in a program using the ldd command, as shown below for a program named hypotenuse (this an executable file i.e. binary machine code) :

prompt> ldd hypotenuse
      	linux-gate.so.1 =>  (0xffffe000)
libm.so.6 => /lib/libm.so.6 (0xb7f95000)
libc.so.6 => /lib/libc.so.6 (0xb7e75000)
/lib/ld-linux.so.2 (0xb7fe6000)