Kenwood HP 9000 Personal Computer User Manual


 
Chapter 2 47
What Happens When You Compile and Link a Program
Linking with Libraries
$ f77 -v sumnum.f
...
/usr/ccs/bin/ld -x /opt/langtools/lib/crt0.o \
sumnum.o -lcl -lisamstub -lc
The Default Library Search Path
By default, ld searches for libraries in the directory /usr/lib. (If the
-p or -G compiler profiling option is specified on the command line, the
compiler directs the linker to also search /usr/lib/libp.) The default
order can be overridden with the LPATH environment variable or the -L
linker option. LPATH and -L are described in “Changing the Default
Library Search Path with -L and LPATH” on page 57.
Link Order
The linker searches libraries in the order in which they are specified on
the command line — the link order. Link order is important in that a
library containing an external reference to another library must precede
the library containing the definition. This is why libc is typically the
last library specified on the linker command line: because the other
libraries preceding it in the link order often contain references to libc
routines and so must precede it.
NOTE If multiple definitions of a symbol occur in the specified libraries, ld does
not necessarily choose the first definition. It depends on whether the
program is linked with archive libraries, shared libraries, or a
combination of both. Depending on link order to resolve such library
definition conflicts is risky because it relies on undocumented linker
behavior that may change in future releases. (See Also “Caution When
Mixing Shared and Archive Libraries” on page 164.)