libc is always searched automatically. libc.a is an archive library. It contains object files for every function in the standard C library. When you call a function in that library, only the object file in the archive that contains the called function is incorporated in your executable at linking time. Dynamically linked libraries are configured differently. libc.so is a single object file that contains the code for every function in the dynamically linked C library. When you call a function in that library, and dynamically link your program with it, the entire contents of libc.so are mapped into the virtual address space of your process at run time.
If you need to point the link editor to
standard libraries that are not searched automatically,
you specify the -l option explicitly
on the cc command line.
As shown previously, -lx directs the link editor
to search the dynamically linked library libx.so
or the archive library libx.a.
So if your program calls the function sin(),
for example, in
the standard math library libm,
cc -dy file1.c file2.c file3.c -lm
will direct the link editor to search for /usr/ccs/lib/libm.so and, if it does not find it, /usr/ccs/lib/libm.a to satisfy references to sin() in your program. Because the compilation system does not supply a dynamically linked library version of libm, the above command will direct the link editor to search libm.a unless you have installed a dynamically linked library version of libm in the standard place. Note that because dynamic linking was turned on with the -dy option, the above command will direct the link editor to first search libc.so rather than libc.a. Use the same command without the -dy option to link your program statically with libm.a and libc.a. The contents of libm are described in the second part of this chapter.
Note that because the link editor searches an archive library
only to resolve undefined external references it has previously seen,
the placement of the -l option on the
cc command line is important.
cc file1.c -lm file2.c file3.c
will direct the link editor to search libm.a
only for definitions that satisfy still unresolved
external references in file1.c.
As a rule, then, it's best to put -l at the end of
the command line.