Sun Studio 12: Fortran Programming Guide

4.3.3 Library Search Path and Order—Static Linking

Use the -llibrary compiler option to name additional libraries for the linker to search when resolving external references. For example, the option -lmylib adds the library libmylib.so or libmylib.a to the search list.

The linker looks in the standard directory paths to find the additional libmylib library. The -L option (and the LD_LIBRARY_PATH environment variable) creates a list of paths that tell the linker where to look for libraries outside the standard paths.

Were libmylib.a in directory /home/proj/libs, then the option–L/home/proj/libs would tell the linker where to look when building the executable:


demo% f95 -o pgram part1.o part2.o -L/home/proj/libs -lmylib

4.3.3.1 Command-Line Order for -llibrary Options

For any particular unresolved reference, libraries are searched only once, and only for symbols that are undefined at that point in the search. If you list more than one library on the command line, then the libraries are searched in the order in which they are found on the command line. Place -llibrary options as follows:

4.3.3.2 Command-Line Order for -Ldir Options

The -Ldir option adds the dir directory path to the library search list. The linker searches for libraries first in any directories specified by the -L options and then in the standard directories. This option is useful only if it is placed preceding the–llibrary options to which it applies.