|
|
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.