C++ User's Guide

Using Class Libraries

Generally, two steps are involved in using a class library. First, include the appropriate header in your source code. Second, link your program with the object library.

The iostream Library

The C++ 5.0 compiler provides two implementations of iostreams:

If you have existing C++ sources, your code might look like the following example, which uses classic iostreams.


// file prog1.cc
#include <iostream.h>

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}

The following command compiles in compatibility mode and links prog1.cc into an executable program called prog1. The classic iostream library is part of libC, which is linked by default in compatibility mode.


demo% CC -compat prog1.cc -o prog1

The next example uses standard iostreams.


// file prog1.cc
#include <iostream>

int main() {
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

The following command compiles and links prog2.cc into an executable program called prog2. The program is compiled in standard mode and libCstd, which includes the standard iostream library, is linked by default.


demo% CC prog2.cc -o prog2

The complex Library

The standard library provides a templatized complex library that is similar to the complex library provided with the C++ 4.2 compiler. If you compile in standard mode, you must use <complex> instead of <complex.h>. You cannot use <complex> in compatibility mode.

In compatibility mode, you must explicitly ask for the complex library when linking. In standard mode, the complex library is included in libCstd, and is linked by default.

There is no complex.h header for standard mode. With C++ 4.2, "complex" is the name of a class, but in standard C++, "complex" is the name of a template. It is not possible to provide typedefs that would allow old code to work unchanged. Therefore, code written for 4.2 that uses complex numbers will need some straightforward editing to work with the standard library. For example, the following code was written for 4.2 and will compile in compatibility mode.


// file ex1.cc (compatibility mode)
#include <iostream.h>
#include <complex.h>

int main()
{
    complex x(3,3), y(4,4);
    complex z = x * y;
    cout << "x=" << x << ", y=" << y << ", z=" << z << endl;
}

  

The following example compiles and links ex1.cc in compatibility mode, and then executes the program.


demo% CC -compat ex1.cc -library=complex
demo% a.out
x=(3, 3), y=(4, 4), z=(0, 24)

Here is ex1.cc rewritten as ex2.cc to compile in standard mode:


// file ex2.cc (ex1.cc rewritten for standard mode)
#include <iostream>
#include <complex>
int main()
{
     std::complex<double> x(3,3), y(4,4);
     std::complex<double> z = x * y;
     std::cout << "x=" << x << ", y=" << y << ", z=" << z <<        std::endl;
}

The following example compiles and links the rewritten ex2.cc in standard mode, and then executes the program.


% CC ex2.cc
% a.out
x=(3,3), y=(4,4), z=(0,24)

Linking C++ Libraries

The following table shows the compiler options for linking the C++ libraries. See "-library=l[,...l]" for more information.

Library 

Compile Mode 

Option 

Classic iostream

-compat=4

-compat=5

None needed 

-library=iostream

complex

-compat=4

-compat=5

-library=complex

None needed 

Tools.h++ v7 

-compat=4

-compat=5

-library=rwtool7

-library=rwtool7,iostream

Tools.h++ v7 debug 

-compat=4

-compat=5

-library=rwtool7_dbg

-library=rwtool7_dbg,iostream

Garbage collection 

-compat=4

-compat=5

-library=gc

-library=gc

Garbage collection debug 

-compat=4

-compat=5

-library=gc_dbg

-library=gc_dbg