HPlogo HP C++ Programmer's Guide: HP 9000 Series Workstations and Servers > Chapter 3 Compiling and Executing HP C++ Programs

System Library and Header Files

» 

Technical documentation

Complete book in PDF

 » Table of Contents

 » Index

This section discusses the two types of libraries provided with HP C++:

  • standard HP-UX libraries

  • HP C++ run-time libraries

Standard HP-UX Libraries

There are several libraries providing system services that are included with HP-UX. You can access HP-UX standard libraries by using header files that declare interfaces to those libraries. These library routines are documented in the HP-UX Reference Manual.

Location of Standard HP-UX Header Files

The standard HP-UX header files are located in /usr/include.

To use a system library function, your HP C++ source code must include the preprocessor directive #include. For example,

#include <filename.h>

where filename.h is the name of the C++ header file for the library function you want to use. By enclosing filename.h in angle brackets, the HP C++ preprocessor looks for that particular header file in a standard location on the system. The HP C++ preprocessor first looks for header files in /usr/include/CC (in /opt/CC/include for HP-UX 10.x C++ versions); if any are not found, it then searches /usr/include.

You can use -Idir options to modify the search path. If the -Idir option is specified, the HP C++ preprocessor first looks for #include files in the directories specified in dir before looking into the standard include directories.

Example of Using a Standard Header File

If you want to use the getenv function that is in the standard library files /lib/libc.sl and /lib/libc.a (or /usr/lib/libc.sl and /usr/lib/libc.a for HP-UX 10.x C++ versions), you should specify

#include <stdlib.h>

because the external declaration of getenv is found in the header file /usr/include/stdlib.h.

C++ Run-Time Libraries

In addition to standard HP-UX system libraries, HP C++ provides the following C++ run-time libraries:

Stream Library

The stream library includes class libraries for buffering and formatting I/O operations. It consists of several main I/O classes providing the fundamental facility for I/O conversion and buffering. The stream library also provides classes derived from main classes offering extended I/O functionality such as in-memory formatting and file I/O. For more detailed documentation of this library, refer to the C++ Language System Library Manual.

Ostream Library

The Ostream library is no longer provided with HP C++. It was provided with version 2.1 for backward compatibility with the AT&T C++ version 1.2 stream I/O library. The newer C++ stream library (available since version 2.0) is mostly upward compatible with the older stream library, but there are a few places where differences may affect programs. These differences are discussed in chapter 3 of the C++ Language System Library Manual, under "Converting from Streams to Iostreams."

Task Library

The task library is a multiple threaded, co-routine class library that enables users to simulate, control, and model UNIX system processes in an object-oriented paradigm. This library also encapsulates reusable tasking primitives such as the scheduler, task queue, timer, and interrupt handler. The task library is useful for simulations or pseudo parallel-processing algorithms. For more detailed documentation of this library, refer to the C++ Language System Library Manual.

Complex Library

The complex library implements the data type of complex numbers as a class complex. It overloads the standard input, output, arithmetic, assignment, and comparison operations. It also defines the standard exponential, logarithm, power, and square-root functions, as well as the trigonometric functions of sine, cosine, hyperbolic sine, and hyperbolic cosine. For more detailed documentation of this library, refer to the C++ Language System Library Manual.

HP Codelibs Library

The HP Codelibs library contains many general-purpose classes you can use in your applications, including:

  • strings

  • dynamic arrays

  • sets

  • hash tables

  • shared memory management routines

  • memory allocation

  • lists

The header files for the HP Codelibs library are in the directory /usr/include/codelibs (/opt/CC/include/codelibs for HP-UX 10.x C++ versions). Use -I/usr/include/codelibs to direct the compiler to search these header files. For more information about this library, refer to the Codelibs Library Reference - Version 2.100.

Standard Components Library

The USL C++ Standard Components library contains many general-purpose classes you can use in your applications, including:

  • dynamic arrays

  • graphs

  • lists

  • memory allocation

  • sets

  • bags

  • strings

The header files for the Standard Components library are in the directory /usr/include/SC (/opt/CC/include/SC for HP-UX 10.x C++ versions). Use -I/usr/include/SC to direct the compiler to search these header files.

A collection of program development tools for use with the Standard Components library are in /usr/bin. These include hier, incl, publik, dem, and g2++comp.

For more information about the Standard Components, refer to the USL C++ Standard Components Manual. To see the online manual pages, first add the directory /usr/CC/man/SC (/opt/CC/share/man for HP-UX 10.x C++ versions) to your MANPATH environment variable. Then type man name where name is a particular manual page. For an introduction to Standard Components, type man SC_intro and man SC_tools_intro.

Locations of Library Files

Table 3-2 “HP C++ Library Files” lists the files containing the HP C++ run-time libraries.

Different libraries are used depending on whether or not you use exception handling.

Table 3-2 HP C++ Library Files

LibraryDefault File Exception Handling File
Stream library/usr/lib/libC.a/usr/lib/libC.sl/usr/lib/libC.ansi.a/usr/lib/libC.ansi.sl/usr/lib/CC/eh/libC.a/usr/lib/CC/eh/libC.ansi.a
Task library/usr/lib/libtask.a/usr/lib/libtask.sl Not available.
Complex library/usr/lib/libcomplex.a/usr/lib/CC/eh/libcomplex.a
Demangling library /usr/lib/libdemangle.a Not available.
Codelibs library/usr/lib/libcodelibs.a/usr/lib/libcodelibs.sl/usr/lib/CC/eh/libcodelibs.a
Standard Components library/usr/lib/lib++.a/usr/lib/libGA.a/usr/lib/libGraph.a/usr/lib/libfs.a/usr/lib/libg2++.a/usr/lib/incl2/usr/lib/hier2/usr/lib/publik2/usr/lib/CC/eh/lib++.a/usr/lib/CC/eh/libGA.a/usr/lib/CC/eh/libGraph.a/usr/lib/CC/eh/libfs.a/usr/lib/CC/eh/libg2++.a/usr/lib/CC/eh/incl2/usr/lib/CC/eh/hier2/usr/lib/CC/eh/publik2

 

NOTE: HP-UX 10.x C++ versions store library files in /opt/CC/lib.

Support for Multi-Threaded Applications

For HP-UX 10.x C++ versions, the HP C++ run-time environment supports multi-threaded applications. The following HP C++ libraries are thread-safe:

  • libC.ansi.sl and libC.ansi.a

  • libC.sl and libC.a

  • libcxx.a

  • libcomplex.a

There are no interface changes to the functions in these libraries nor in the standard include files in /usr/include and /opt/CC/include/CC. However, there are new reentrant versions of the functions chr_r, str_r, form_r, hex_r, dec_r, and oct_r. In order to link with these functions, you must include the stream.h header file in your source file and add the -D_THREAD_SAFE compile time flag to your compilation line.

Also, in order to pick the thread safe version of the I/O routines, cout, cin, and cerr, you must include iostream.h in your source files and add the -D_THREAD_SAFE compile time flag to your compilation line. To guarantee that your I/O results from one thread are not intermingled with I/O results from other threads, you should protect your I/O statement with locks. For example:

    // create a mutex and initialize it

    pthread_mutex_t the_mutex;

    pthread_mutex_init(&the_mutex, pthread_mutexattr_default);



    pthread_mutex_lock(&the_mutex);

      cout << "something" ... ;

    pthread_mutex_unlock(&_the_mutex);

There are no new compiler options for compiling multi-threaded programs.

C++ Library Header Files

HP C++ includes the following header files for interface to C++ run-time libraries:

  • complex.h — implementation of complex numbers class

  • generic.h — error handling and string concatenation macros

  • iostream.h — I/O streams classes ios, istream, ostream, and streambuf

  • fstream.h — I/O streams specialized for files

  • strstream.hStreambuf specialized to arrays

  • iomanip.h — predefined manipulators and macros

  • stdiostream.h — specialized streams and streambufs for interaction with stdio

  • stream.h — includes iostream.h, fstream.h, stdiostream.h and iomanip.h for compatibility with AT&T USL C++ version 1.2

  • vector.h — macros for class declaration and constructor definition for vectors

  • task.h — implementation of task class

  • dem.h — routines for demangling encoded C++ names (not compatible with previous version)

  • eh.h — exception handling routines

  • new.h — dynamic memory routines new and set_new_handler

For more detailed documentation on these headers, refer to C++ Language System Library Manual. For information on the header files for the HP Codelibs library, refer to the Codelibs Library Reference - Version 2.100.

For information on the header files for the Standard Components library, refer to the USL C++ Standard Components Manual.

Location of C++ Header Files

The above header files are located in the directory /usr/include/CC. The header files for the HP Codelibs library are in the directory /usr/include/codelibs. (In /opt/CC/include/codelibs for HP-UX 10.x C++ versions.)

Use -I/usr/include/codelibs to direct the compiler to search these header files. The header files for the Standard Components library are in the directory /usr/include/SC. (In /opt/CC/include/SC for HP-UX 10.x C++ versions.) Use -I/usr/include/SC to direct the compiler to search these header files.

Example of Using a C++ Header File

If, for example, you want to use complex numbers in your application, you must specify the following:

#include <complex.h>

Linking to C++ Libraries

You can compile and link any C++ modules to one or more libraries. HP C++ automatically links

/usr/lib/libC.sl

(the HP C++ run-time library, including the stream library) and

/lib/libc.sl

(the HP-UX system library) with a C++ program.

The ANSI C versions of the C++ run-time library are also included, libC.ansi.sl and libC.ansi.a. If you have compiled with the +a1 option, you must also pass +a1 to the CC command when linking to make sure the linker uses these libraries.

If you want archive libraries instead of shared libraries, use the -a,archive linker option. (See the section "Linking Archive or Shared Libraries" later in this chapter for more information.)

You can specify other libraries using the -l option. For example, in order to use the complex library, you must specify -lcomplex:

CC complex_appl.C -lcomplex

Your C++ run-time library may require that additional HP-UX standard libraries be specified. For example, the complex library uses the HP-UX math library for mathematical functions. So, for example, you might need to specify -lm for the math library:

CC complex_appl.C -lcomplex -lm
© Hewlett-Packard Development Company, L.P.