140 Chapter 5
Creating and Using Libraries
Creating Shared Libraries
$ cc -Aa -c +z length.c volume.c mass.c
length.c:
volume.c:
mass.c:
$ ld -b -o libunits.sl length.o volume.o mass.o
Once the library is created, be sure it has read and execute permissions
for all users who will use the library. For example, the following chmod
command allows read/execute permission for all users of the
libunits.sl library:
$ chmod +r+x libunits.sl
This library can now be linked with other programs. For example, if you
have a C program named convert.c that calls routines from
libunits.sl, you could compile and link it with the cc command:
$ cc -Aa convert.c libunits.sl
In 32-bit mode, once the executable is created, the library should not be
moved because the absolute path name of the library is stored in the
executable. (In 64-bit mode, ./libunit.sl is stored in the
executable.)For details, see “Shared Library Location”.
For details on linking shared libraries with your programs, see Chapter
3, “Linker Tasks,” on page 51.
NOTE If you are linking any C++ object files to create an executable or a shared
library, you must use the CC command to link. This ensures that
c++patch executes and chains together your nonlocal static constructors
and destructors. If you use ld, the library or executable may not work
correctly and you will probably not get any error messages. For more
information see the HP C++ Programmer's Guide.
Shared Library Dependencies
You can specify additional shared libraries on the ld command line when
creating a shared library. The created shared library is said to have a
dependency on the specified libraries, and these libraries are known as
dependent libraries or supporting libraries. When you load a
library with dependencies, all its dependent libraries are loaded too. For
example, suppose you create a library named libdep.sl using the
command:
$ ld -b -o libdep.sl mod1.o mod2.o -lcurses -lcustom