JavaScript is required to for searching.
Skip Navigation Links
Exit Print View
Oracle Solaris Studio 12.2: C++ User's Guide
search filter icon
search icon

Document Information

Preface

Part I C++ Compiler

1.  The C++ Compiler

2.  Using the C++ Compiler

3.  Using the C++ Compiler Options

Part II Writing C++ Programs

4.  Language Extensions

5.  Program Organization

6.  Creating and Using Templates

7.  Compiling Templates

8.  Exception Handling

9.  Improving Program Performance

10.  Building Multithreaded Programs

Part III Libraries

11.  Using Libraries

11.1 The C Libraries

11.2 Libraries Provided With the C++ Compiler

11.2.1 C++ Library Descriptions

11.2.2 Accessing the C++ Library Man Pages

11.2.3 Default C++ Libraries

11.3 Related Library Options

11.4 Using Class Libraries

11.4.1 The iostream Library

11.4.2 The complex Library

11.4.3 Linking C++ Libraries

11.5 Statically Linking Standard Libraries

11.6 Using Shared Libraries

11.7 Replacing the C++ Standard Library

11.7.1 What Can Be Replaced

11.7.2 What Cannot Be Replaced

11.7.3 Installing the Replacement Library

11.7.4 Using the Replacement Library

11.7.5 Standard Header Implementation

11.7.5.1 Replacing Standard C++ Headers

11.7.5.2 Replacing Standard C Headers

12.  Using The C++ Standard Library

13.  Using the Classic iostream Library

14.  Using the Complex Arithmetic Library

15.  Building Libraries

Part IV Appendixes

A.  C++ Compiler Options

B.  Pragmas

Glossary

Index

11.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.

11.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 a full discussion of compilation modes, see the C++ Migration Guide.

11.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 13-4.

11.4.3 Linking C++ Libraries

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

Table 11-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
Apache stdcxx version 4
-compat=5
-library=stdcxx4