next up previous
Next: Handy Macro: assert() Up: intro_C Previous: Compiling

Libraries and Header Files

To keep C as portable as possible many machine-specific tasks are kept in separate libraries. To use these library functions you must first include an appropriate header file that contains various declarations and definitions. For example, to use printf() you must #include <stdio.h> at the top of the source file. Other common header files include <stdlib.h> (for less-used functions like malloc() and rand()) and <math.h> (for certain math functions, like sqrt() and pow()). In some instances you must also ``link'' to the corresponding library when compiling. The most common library needed is the math library, which is linked by appending ``-lm'' to the compile line (e.g. gcc myprog.c -lm). Most C library functions have corresponding Unix man pages with synopses of header file and linkage requirements. Note that the order in which libraries are linked is sometimes important; it's usually a good idea to put ``-lm'' last, since other libraries may depend on the math library as well.

Massimo Ricotti 2009-01-26