HP-UX Linker and Libraries User's Guide
Following are the commands you can use to create a shared library called libunits.so:
$ cc -Aa -c +z length.c volume.c mass.c
$ ld -b -o libunits.so length.o volume.o mass.o
Other topics relevant to shared libraries are:
• “Shared Library Dependencies” (page 100)
• “Updating a Shared Library” (page 102)
• “Shared Library Location (IPF)” (page 102)
• “Improving Shared Library Performance” (page 102)
• “Function Level Versioning” (page 110)
Creating Position-Independent Code (PIC)
In PA-32 mode, the first step in creating a shared library is to create object files containing
position-independent code (PIC). There are two ways to create PIC object files:
• Compile source files with the +z or +Z compiler option, described below.
• Write assembly language programs that use appropriate addressing modes, described in
“Writing and Generating Position-Independent Code” (page 185) .
In PA-32 mode, the +z and +Z options force the compiler to generate PIC object files. In PA-64
and IPF mode, the +Z option is the default.
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 can
use this command:
$ cc -Aa -c +z length.c The +z option creates PIC.
You can then link it with other PIC object files to create a shared library, as discussed in “Creating
the Shared Library with ld” (page 99) .
Comparing +z and +Z
In PA-32 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 PA-64 and IPF mode, +Z is the default and the compilers ignore the options and generate PIC
code.
Compiler Support for +z and +Z
In PA-32 mode, the C, C++, FORTRAN, and Pascal compilers support the +z and +Z options.
In PA-64 and IPF 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 names 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, then combine the resulting
.o files with ld. Following are the commands you can use to create a shared library named
libunits.so:
Creating Shared Libraries 99