Pass Option to System Linker Changing Search Algorithm and Maintaining Relative Ordering (-L dir)

Changes the order in which the system linker searches for libraries that were specified with the -l cob flag. -L options affect only those -l options that they precede on the cob command line.

The -L cob flag is effectively the same as the -L option for the system linker, ld (see your operating system documentation for further details on this linker option). Normally, ld will search for libraries only in a predefined set of default directories; typically /usr/lib and /lib, but this varies from system to system. However, specifying the -L flag causes the system linker instead to search the specified directory first for such libraries. The order in which -L options are specified is significant as directories are searched in that order.

Paths in the environment variable LD_LIBRARY_PATH, or LIBPATH (AIX only) are searched after -L, +L directories but before the default paths.


The following example illustrates the use of the +L and -L flags in determining the order in which directories are searched for libraries:

cob +L /usr/mylib +llib1 object.o -L /usr/grouplib -llib2 +llib3

The relative ordering of the options passed to the system linker is:

  1. object.o
  2. -L/usr/grouplib
  3. -llib2
  4. -L/usr/mylib
  5. -llib1
  6. -llib3

Note that the ‘+’ options become ‘-’ options when passed to ld, and move to the end of the list.

This affects the order in which ld searches for libraries, and in which directory. The principle is that ld searches for a library (specified by –lname) in the following order:

  1. -L directories specified before –l on the command line, in the order in which they were specified
  3. the default directories – typically /usr/lib but varies significantly from operating system to operating system

AIX does not use LD_LIBRARY_PATH, but uses LIBPATH.

ld searches for libraries in the above example using (directory, library) combinations in the following order:

  1. -L/usr/grouplib -llib2
  2. $LD_LIBRARY_PATH -llib2
  3. default-directories -llib2
  4. -L/usr/grouplib -llib1
  5. -L/usr/mylib -llib1
  6. $LD_LIBRARY_PATH -llib1
  7. default-directories -llib1
  8. -L/usr/grouplib -llib3
  9. -L/usr/mylib -llib3
  10. $LD_LIBRARY_PATH -llib3
  11. default-directories -llib3

The complete search path for every library would be searched only if a status of "file not found" is returned on earlier paths.