C compilation system

Specifying directories to be searched by the dynamic linker

When you use the -l option, you must point the dynamic linker to the directories of the dynamically linked libraries that are to be linked with your program at execution. The environment variable LD_RUN_PATH lets you do that at link time. To set LD_RUN_PATH, list the absolute pathnames of the directories you want searched in the order you want them searched. Separate pathnames with a colon, as shown in the following example:

LD_RUN_PATH=/home/mylibs export LD_RUN_PATH

The command:

cc -dy -o prog -L/home/mylibs file1.c file2.c file3.c -lfoo

directs the dynamic linker to search for in /home/mylibs when you execute your program:


The dynamic linker searches the standard place by default, after the directories you have assigned to LD_RUN_PATH. Note that as far as the dynamic linker is concerned, the standard place for libraries is /usr/lib. Any executable versions of libraries supplied by the compilation system kept in /usr/lib.

The environment variable LD_LIBRARY_PATH lets you do the same thing at run time. Suppose you have moved to /home/sharedobs. It is too late to replace /home/mylibs with /home/sharedobs in LD_RUN_PATH, at least without link editing your program again. You can, however, assign the new directory to LD_LIBRARY_PATH, as follows:

LD_LIBRARY_PATH=/home/sharedobs export LD_LIBRARY_PATH

Now when you execute your program


the dynamic linker searches for first in /home/mylibs and, not finding it there, in /home/sharedobs. The directory assigned to LD_RUN_PATH is searched before the directory assigned to LD_LIBRARY_PATH. The important point is that because the pathname of is not hard-coded in prog, you can direct the dynamic linker to search a different directory when you execute your program. You can move a dynamically linked library without breaking your application.

You can set LD_LIBRARY_PATH without first having set LD_RUN_PATH. The main difference between them is that once you have used LD_RUN_PATH for an application, the dynamic linker searches the specified directories every time the application is executed (unless you have relinked the application in a different environment). In contrast, you can assign different directories to LD_LIBRARY_PATH each time you execute the application. LD_LIBRARY_PATH directs the dynamic linker to search the assigned directories before it searches the standard place. Directories, including those in the optional second list, are searched in the order listed. See the previous section for the syntax.

Note, finally, that when linking a set-user or set-group program, the dynamic linker ignores any directories that are not built into the dynamic linker. Currently, the only built-in directory is /usr/lib.

Next topic: Checking for run-time compatibility
Previous topic: Specifying directories to be searched by the link editor

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003