88 Chapter 3
Linker Tasks
Using Linker commands
The x argument corresponds to the identifying portion of the library path
name — the part following lib and preceding the suffix .a or .sl. For
example, for the libm.sl or libm.a library, x is the letter m:
$ cc -Aa mathprog.c -lm
The linker searches libraries in the order in which they are specified on
the command line (that is, the link order). In addition, libraries
specified with -l are searched before the libraries that the compiler links
by default.
Using the -l: option
The -l: option works just like the -l option with one major difference:
-l: allows you to specify the full basename of the library to link with.
For instance, -l:libm.a causes the linker to link with the archive
library /usr/lib/libm.a, regardless of whether -a shared was
specified previously on the linker command line.
The advantage of using this option is that it allows you to specify an
archive or shared library explicitly without having to change the state of
the -a option. (See also “Caution When Mixing Shared and Archive
Libraries” on page 164.)
For instance, suppose you use the LDOPTS environment variable (see
“Passing Linker Options with LDOPTS”) to set the -a option that you
want to use by default when linking. And depending on what
environment you are building an application for, you might set LDOPTS
to -a archive or -a shared. You can use -l: to ensure that the linker
will always link with a particular library regardless of the setting of the
-a option in the LDOPTS variable.
Example Using -l:
For example, even if LDOPTS were set to -a shared, the following
command would link with the archive libfoo.a in the directory
/usr/mylibs, the archive libm.a and libc.a:
$ ld /opt/langtools/lib/crt0.o -u main prog.o -L/usr/mylibs \
-l:libfoo.a -l:libc.a -l:libm.a