Kenwood HP 9000 Personal Computer User Manual


 
Chapter 3 83
Linker Tasks
Using Linker commands
When linking with other libraries (to create an executable), it ensures
that the library will use the local definition of a routine rather than a
definition that occurs earlier in the link order.
Exporting a symbol is necessary if the symbol must be accessible outside
the shared library. But remember that, by default, most symbols are
global definitions anyway, so it is seldom necessary to explicitly export
symbols. In C, all functions and global variables that are not explicitly
declared as static have global definitions, while static functions and
variables have local definitions. In FORTRAN, global definitions are
generated for all subroutines, functions, and initialized common blocks.
When using +e, be sure to export any data symbols defined in the shared
library that will be used by another shared library or the program, even
if these other files have definitions of the data symbols. Otherwise, your
shared library will use its own private copy of the global data, and
another library or the program file will not see any change.
One example of a data symbol that should almost always be exported
from a shared library is errno. errno is defined in every shared library
and program; if this definition is hidden, the value of errno will not be
shared outside of the library.
Hiding Symbols When Combining .o Files with the -r
Option
The -r option combines multiple .o files, creating a single .o file. The
reasons for hiding symbols in a .o file are the same as the reasons listed
above for shared libraries. However, a performance improvement will
occur only if the resulting .o file is later linked into a shared library.
Hiding and Exporting Symbols When Creating an
a.out File
By default, the linker exports all of a program's global definitions that
are imported by shared libraries specified on the linker command line.
For example, given the following linker command, all global symbols in
crt0.o and prog.o that are referenced by libm or libc are
automatically exported:
$ ld /usr/ccs/lib/crt0.o prog.o -lm -lc
With libraries that are explicitly loaded with shl_load, this behavior
may not always be sufficient because the linker does not search explicitly
loaded libraries (they aren't even present on the command line). You can
work around this using the -E or +e linker option.