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

Quick-reference guide

The following are general link editing conventions that you must be familiar with:

  1. By convention, shared objects, or dynamically linked libraries, 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. libc.so, then, is the shared object version of the standard C library; libc.a is the archive version.

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

  3. By default, the link editor chooses the shared object implementation of a library, libx.so, in preference to the archive library implementation, 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 dynamically linked with libc.so automatically:
   $ cc file1.c file2.c file3.c

To link your program statically with libc.a, turn off the dynamic linking default with the -dn option:

   $ cc -dn 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
will direct the link editor to search for libx.so, then libx.a in the standard place. Note 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 -Ldir 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 shared object 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
It's best to 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.

To direct the link editor to search libx.a where libx.so exists in the same directory, turn off the dynamic linking default with the -dn option:

   $ cc -dn -Ldir file1.c file2.c file3.c -lx
That command will direct the link editor to search libc.a well as libx.a. 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 -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: Accessing C functions from C++

© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004