Sun Studio 12 Update 1: C++ User's Guide

12.4 Using Class Libraries

Generally, two steps are involved in using a class library:

  1. Include the appropriate header in your source code.

  2. Link your program with the object library.

12.4.1 The iostream Library

The C++ 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.


example% CC -compat prog1.cc -o prog1

The next example uses standard iostreams.


// file prog2.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.


example% CC prog2.cc -o prog2

For more information about libCstd, see Caveats:. For more information about libiostream, see 13.3.1 Redistribution and Supported STLport Libraries.

For a full discussion of compilation modes, see the C++ Migration Guide.

12.4.2 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. In 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 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.


example% CC -compat ex1.cc -library=complex
example% 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>
using std::complex;

int main()
{
     complex<double> x(3,3), y(4,4);
     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)

For more information about using the complex arithmetic library, see Table 14–4.

12.4.3 Linking C++ Libraries

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

Table 12–2 Compiler Options for Linking C++ Libraries

Library 

Compilation Mode 

Option  

Classic iostream

–compat=4

–compat=5

None needed 

-library=iostream

complex

–compat=4

-compat=5

-library=complex

None needed 

Tools.h++ version 7

–compat=4

–compat=5

-library=rwtools7

-library=rwtools7,iostream

-library=rwtools7_std

Tools.h++ version 7 debug

–compat=4

–compat=5

-library=rwtools7_dbg

-library=rwtools7_dbg,iostream

-library=rwtools7_std_dbg

Garbage collection

–compat=4

–compat=5

-library=gc

-library=gc

STLport version 4 

–compat=5

-library=stlport4

STLport version 4 debug 

–compat=5

-library=stlport4_dbg