tags:

views:

56

answers:

1

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.

+1  A: 

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.

Vlad Lazarenko
Just to confirm, is that right that LD_LIBRARY_PATH is considered after all -L options?
Leonid
No, not really. It depends whether it contains a semicolon or not. If it doesn't then it goes after all -L, otherwise list before semicolon goes first, then all -L options, then list after a semicolon. I will update my answer, because it is hard to find this in that big document. Also note that this applies to Sun linker, not GNU's one. I am not sure which one you are using.
Vlad Lazarenko