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

Quick-reference guide

  1. By convention, dynamically linked libraries, or shared objects, are designated by the prefix lib and the suffix .so; archives, or statically linked libraries, are designated by the prefix lib and the suffix .a. The archive version of the standard C library is libc.a; libc.so is the dynamically linked library version.

  2. These conventions are recognized, in turn, by the -l option to the cc command. -lx directs the link editor to search the archive library libx.a or the dynamically linked library libx.so. The cc command automatically passes -lc to the link editor. Therefore, the compilation system arranges for the standard C library to be linked with your program transparently.

  3. By default, the link editor chooses the archive library implementation, libx.a. Use option -y to direct the link editor to choose the dynamically linked library implementation of a library, libx.so, in preference to libx.a in the same directory.

  4. By default, the link editor searches for libraries in the standard places on your system, /usr/ccs/lib and /usr/lib, in that order. The standard libraries supplied by the compilation system normally are kept in /usr/ccs/lib.

In this arrangement, then, C programs are statically linked with libc.a automatically:

cc file1.c file2.c file3.c

To link your program dynamically with libc.so, turn on dynamic linking with the -dy option:

cc -dy file1.c file2.c file3.c

Specify the -l option explicitly to link your program with any other library. If the library is in the standard place, the command:

cc file1.c file2.c file3.c -lx

directs the link editor to search for libx.a in the standard place. With the -dy option, the command:

cc -dy file1.c file2.c file3.c -lx

directs the link editor to search for libx.so, then libx.a in the standard place. Note that the compilation system supplies dynamically linked library version of libc. (Other dynamically linked libraries are supplied with the operating system, and usually are kept in the standard places.) Note, too, that, as a rule, it's best to place -l at the end of the command line.

If the library is not in the standard place, specify the path of the directory in which it is stored with the -L option:

cc -L dir file1.c file2.c file3.c -lx

or the environment variable LD_LIBRARY_PATH:

LD_LIBRARY_PATH= dir export LD_LIBRARY_PATH
cc file1.c file2.c file3.c -lx

If the library is a dynamically linked library and is not in the standard place, you must also specify the path of the directory in which it is stored with either the environment variable LD_RUN_PATH at link time, or the environment variable LD_LIBRARY_PATH at run time:

LD_RUN_PATH=dir export LD_RUN_PATH
$ LD_LIBRARY_PATH=dir export LD_LIBRARY_PATH

Use an absolute path when you set these environment variables. Note that LD_LIBRARY_PATH is read both at link time and at run time.

By default, the link editor ignores libx.so. To direct the link editor to search libx.so and then libx.a in the same directory, turn on dynamic linking with the -dy option:

cc -dy -Ldir file1.c file2.c file3.c -lx

That command directs the link editor to search libc.so well as libx.so. To link your program statically with libx.a and dynamically with libc.so, use the -Bstatic and -Bdynamic options to turn dynamic linking off and on:

cc -dy -Ldir file1.c file2.c file3.c -Bstatic -lx -Bdynamic

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.


Next topic: Libraries and header files
Previous topic: Multiply defined symbols

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