Chapter 3 53
Linker Tasks
Using the Compiler to Link
Using the Compiler to Link
In many cases, you use your compiler command to compile and link
programs. Your compiler uses options that directly affect the linker.
Changing the Default Library Search Path
with -Wl, -L
By default, the linker searches the directory /usr/lib and
/usr/ccs/lib for libraries specified with the -l compiler option. (If the
-p or -G compiler option is specified, then the linker also searches the
profiling library directory /usr/lib/libp.)
The -L libpath option to ld augments the default search path; that is, it
causes ld to search the specified libpath before the default places. The C
compiler (cc), the C++ compiler (CC), the POSIX FORTRAN compiler
(fort77), and the HP Fortran 90 compiler (f90) recognize the -L option
and pass it directly to ld. However, the HP FORTRAN compiler (f77)
and Pascal compiler (pc) do not recognize -L; it must be passed to ld
with the -Wl option.
Example Using -Wl, -L
For example, to make the f77 compiler search /usr/local/lib to find
a locally developed library named liblocal, use this command line:
$f77 prog.f -Wl,-L,/usr/local/lib -llocal
(The f77 compiler searches /opt/fortran/lib and /usr/lib as
default directories.)
To make the f90 compiler search /usr/local/lib to find a locally
developed library named liblocal,, use this command line:
$f90 prog.f90 -L/usr/local/lib -llocal
(The f90 compiler searches /opt/fortran90/lib and /usr/lib as
default directories.)
For the C compiler, use this command line:
$ cc -Aa prog.c -L /usr/local/lib -llocal