next up previous contents index
Next: Makefiles Up: Subroutines, functions, and libraries Previous: Header files   Contents   Index


For maintaining large numbers of subroutines, it may be useful to use what is known as a library. For example, suppose we have placed the sources for some number of subroutines in a couple of files, sub1.c and sub2.c. Compiling them as
  cc -c sub1.c
  cc -c sub2.c
will produce two object files, as before. However, rather than including the object files when compiling a main program that uses some of these routines, we can first create a library (or archive) as
  ar -rv libmy_subs.a sub1.o sub2.o
using the ar function on Unix, or as
  tlib my_lib.lib -+sub1.obj
  tlib my_lib.lib -+sub2.obj
using Borland's compiler on Win32. Linking a program against these libraries can then be done as
  cc -o my_prog my_prog.c -L. -lmy_subs
on Unix, or as
  bcc32 -o my_prog.c my_lib.lib
on Win32. Note the use of the -L switch, which can be used to specify directories to include when searching for libraries. Also note the convention followed for library names on Unix - a library called libmy_subs.a is included through the use of the -l switch as -lmy_subs, without the lib prefix and without the tt .a extension.