3. Using the C++ Compiler Options
6. Creating and Using Templates
9. Improving Program Performance
10. Building Multithreaded Programs
11.2 Libraries Provided With the C++ Compiler
11.2.1 C++ Library Descriptions
11.2.2 Accessing the C++ Library Man Pages
11.5 Statically Linking Standard Libraries
11.7 Replacing the C++ Standard Library
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
Generally, two steps are involved in using a class library:
Include the appropriate header in your source code.
Link your program with the object library.
The C++ compiler provides two implementations of iostreams:
Classic iostreams. This term refers to the iostreams library shipped with the C++ 4.0, 4.0.1, 4.1, and 4.2 compilers, and earlier with the cfront-based 3.0.1 compiler. There is no standard for this library. It is part of libC in compatibility mode and is also available in libiostream in the standard mode.
Standard iostreams. This is part of the C++ standard library, libCstd, and is available only in standard mode. It is neither binary-, nor source-compatible with the classic iostreams library.
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.
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.
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
|