Question

It is possible that more than one instance of the library exists in the search path during compilation. In what order will linker process directories included in the search path? The platform in question is Sun OS.

Was it helpful?

Solution

The directories are searched in the order in which they are specified on the command line. Directories specified on the command line are searched before the default directories. All -L options apply to all -l options, regardless of the order in which the options appear. LD_LIBRARY_PATH may also be used to supplement the library search path. The directory search stops at the first directory containing the matching library.

LD_LIBRARY_PATH is tricky though:

LD_LIBRARY_PATH 

A list of directories in which to search for libraries specified with the -l option. Multiple directories are separated by a colon. In the most general case, it will contain two directory lists separated by a semicolon:

dirlist1; dirlist2 

If ld is called with any number of occurrences of -L, as in:

ld . . . -Lpath1 . . . -Lpathn . . .

then the search path ordering is:

         dirlist1 path1 . . . pathn dirlist2 LIBPATH

When the list of directories does not contain a semicolon, it is interpreted as dirlist2.

LD_LIBRARY_PATH is also used to specify library search directories to the runtime linker. That is, if LD_LIBRARY_PATH exists in the environment, the runtime linker will search the directories named in it, before its default directory, for shared objects to be linked with the program at execution.

Please read more about it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top