Chapter 5 139
Creating and Using Libraries
Creating Shared Libraries
Example Using +z
Suppose you have some C functions, stored in length.c, that convert
between English and Metric length units. To compile these routines and
create PIC object files with the C compiler, you could use this command:
$ cc -Aa -c +z length.c The +z option creates PIC.
You could then link it with other PIC object files to create a shared
library, as discussed in “Creating the Shared Library with ld”.
Comparing +z and +Z
In 32-bit mode, the +z and +Z options are essentially the same. Normally,
you compile with +z. However, in some instances — when the number of
referenced symbols per shared library exceeds a predetermined limit —
you must recompile with the +Z option instead. In this situation, the
linker displays an error message and tells you to recompile the library
with +Z.
In 64-bit mode, +Z is the default and the compilers ignore the options
and generate PIC code.
Compiler Support for +z and +Z
In 32-bit mode, the C, C++, FORTRAN, and Pascal compilers support the
+z and +Z options.
In 64-bit mode, +Z is the default for the C and C++ compilers.
Creating the Shared Library with ld
To create a shared library from one or more PIC object files, use the
linker, ld, with the -b option. By default, ld will name the library
a.out. You can change the name with the -o option.
For example, suppose you have three C source files containing routines to
do length, volume, and mass unit conversions. They are named
length.c, volume.c, and mass.c, respectively. To make a shared
library from these source files, first compile all three files using the +z
option, then combine the resulting .o files with ld. Shown below are the
commands you would use to create a shared library named
libunits.sl: