DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C compilation system

Specifying directories to be searched by the link editor

In the previous section you created the archive library libfoo.a and the dynamically linked library libfoo.so. For this example, both of these libraries are stored in the directory /home/mylibs, and the executable is being created in a different directory. This example reflects the way most programmers organize their work on the UNIX operating system.

In order to link your program with either of these libraries, the link editor must access the /home/mylibs directory. Specify the directory's pathname with the -L option:

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

The -L option directs the link editor to search for the libraries named with -l first in the specified directory, then in the standard places. In this example, having found the directory /home/mylibs, the link editor will search libfoo.so rather than libfoo.a. As shown earlier, when the link editor encounters otherwise identically named dynamically linked library and archive libraries in the same directory, it searches the library with the .so suffix first because of the -dy option. For the same reason, it will search libc.so here rather than libc.a. Note that you must specify -L to direct the link editor to search for libraries in your current directory. You can use a period (.) to represent the current directory.

To direct the link editor to search libfoo.a, you can either rely on the compiler defaults or explicitly turn off dynamic linking:

cc -dn -L/home/mylibs file1.c file2.c file3.c -lfoo

Under -dn, the link editor does not accept dynamically linked libraries as input. Here, then, it searches libfoo.a rather than libfoo.so, and libc.a rather than libc.so.

To link your program statically with libfoo.a and dynamically with libc.so, you can do either of two things. First, you can move libfoo.a to a different directory -- /home/archives, for example -- then specify /home/archives with the -L option:

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

As long as the link editor encounters the /home/archives directory before it encounters the /home/mylibs directory, it searches libfoo.a rather than libfoo.so. When otherwise identically named .so and .a libraries exist in different directories, the link editor searches the first one it finds. The same thing is true, by the way, for identically named libraries of either type. If you have different versions of libfoo.a in your directories, the link editor searches the first one it finds.

A better alternative might be to leave libfoo.a where you had it in the first place and use the -Bstatic and -Bdynamic options to turn dynamic linking off and on. The following command links your program statically with libfoo.a and dynamically with libc.so:

cc -dy -L/home/mylibs file1.c file2.c file3.c -Bstatic -lfoo -Bdynamic

When you specify -Bstatic, the link editor will not accept a dynamically linked library as input until you specify -Bdynamic. You can use these options as toggles as often as needed on the cc command line:

cc -dy -L/home/mylibs file1.c file2.c -Bstatic -lfoo \
file3.c -Bdynamic -lsharedob

This command will direct the link editor to search the following libraries:

  1. libfoo.a to resolve still unresolved external references in file1.c and file2.c

  2. libsharedob.so to resolve still unresolved external references in all three files and in libfoo.a

  3. libc.so to resolve still unresolved external references in all three files and the preceding libraries

  4. libc.a to resolve still unresolved external references in all three files, the preceding libraries, and libc.so
Files, including libraries, are searched for definitions in the order they are listed on the cc command line. The standard C library is always searched last.

You can add to the list of directories to be searched by the link editor by using the environment variable LD_LIBRARY_PATH. LD_LIBRARY_PATH must be a list of colon-separated directory names. An optional second list is separated from the first by a semicolon:

LD_LIBRARY_PATH=dir:dir;dir:dir export LD_LIBRARY_PATH

The directories specified before the semicolon are searched, in order, before the directories specified with -L; the directories specified after the semicolon are searched, in order, after the directories specified with -L. Note that you can use LD_LIBRARY_PATH in place of -L altogether. In that case the link editor searches for libraries named with -l first in the directories specified before the semicolon, next in the directories specified after the semicolon, and last in the standard places. You should use absolute pathnames when you set this environment variable.


NOTE: LD_LIBRARY_PATH is also used by the dynamic linker. If LD_LIBRARY_PATH exists in your environment, the dynamic linker searches the directories named in it for dynamically linked libraries to be linked with your program at execution. In using LD_LIBRARY_PATH with the link editor or the dynamic linker, then, you should keep in mind that any directories you give to one you are also giving to the other.


Next topic: Specifying directories to be searched by the dynamic linker
Previous topic: Creating and linking with archive and dynamically linked libraries

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