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

The -Llibpath 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

The LPATH environment variable provides another way to override the default search path. For details, see “Changing the Default Library Search Path with -L, LPATH, and $ORIGIN” (page 30)

28 Determining How to Link Programs or Libraries (Linker Tasks)