Creating a Shared Library

To create a shared library from one or more object files, use the -boption at link time. (The object files must have been compiled with +z or +Z.) The -boption creates a shared library rather than an executable file.

NOTE: Use the aCC command to create a C++ shared library. This ensures that static constructors and destructors are executed at appropriate times.

Example

The following example links util.o and creates the shared library util.so.

aCC -b -o util.so util.o

Using a Shared Library

To use a shared library, include the name of the library in the aCC command line or use the-loption.

The linker links the shared library to the executable file it creates. Once you create an executable file that uses a shared library, do not move the shared library as the dynamic loader (dld.so) cannnot find it.

NOTE: Use the aCC command to link any program that uses a C++ shared library. This ensures that static constructors and destructors in the shared library are executed at appropriate times.

Example

The following example compiles prog.C, links it with the shared library util.so, and creates the executable file a.out.

aCC prog.C util.so

Example of Creating and Using a Shared Library

The following command compiles the two files, Strings.C and Arrays.C, and creates the two object files, Strings.o and Arrays.o. These object files contain position-independent code (PIC):

aCC -c +z Strings.C Arrays.C

The following command builds the shared library, libshape.so, from the object files Strings.oand Arrays.o:

aCC -b -o libshape.so Strings.o Arrays.o

The following command compiles a program, draw_shapes.C, that uses the shared library, libshape.so:

aCC draw_shapes.C libshape.so

Linking Archive or Shared Libraries

When an archive and shared version of a particular library reside in the same directory, the linker links in the shared version by default. You can override this behavior with the -alinker option.

NOTE: Use the +A option when using only archive libraries to create a completely archived executable.

The -aoption identifies the library type for the linker. The -aoption is positional and applies to all subsequent libraries specified with the -loption until the end of the command line or until the next -aoption is encountered. Pass the -aoption to the linker with the -Wx,argsoption.

Creating and Using Libraries 181

Page 181
Image 181
HP C/aC++ for PA-RISC Software Creating a Shared Library, Using a Shared Library, Linking Archive or Shared Libraries