Headers
Prev | Chapter 3. Using | Next

## Headers

### Header Files

The C++ standard specifies the entire set of header files that must be available to all hosted implementations. Actually, the word "files" is a misnomer, since the contents of the headers don't necessarily have to be in any kind of external file. The only rule is that when one `#include`'s a header, the contents of that header become available, no matter how.

That said, in practice files are used.

There are two main types of include files: header files related to a specific version of the ISO C++ standard (called Standard Headers), and all others (TR1, C++ ABI, and Extensions).

Two dialects of standard headers are supported, corresponding to the 1998 standard as updated for 2003, and the draft of the upcoming 200x standard.

C++98/03 include files. These are available in the default compilation mode, i.e. `-std=c++98` or `-std=gnu++98`.

Table 3.1. C++ 1998 Library Headers

`algorithm` | `iomanip` | `list` | `ostream` | `streambuf`
`bitset` | `ios` | `locale` | `queue` | `string`
`complex` | `iosfwd` | `map` | `set` | `typeinfo`
`deque` | `iostream` | `memory` | `sstream` | `utility`
`exception` | `istream` | `new` | `stack` | `valarray`
`fstream` | `iterator` | `numeric` | `stdexcept` | `vector`
`functional` | `limits` |  |  |

Table 3.2. C++ 1998 Library Headers for C Library Facilities

`cassert` | `ciso646` | `csetjmp` | `cstdio` | `ctime`
`cctype` | `climits` | `csignal` | `cstdlib` | `cwchar`
`cerrno` | `clocale` | `cstdarg` | `cstring` | `cwctype`
`cfloat` | `cmath` | `cstddef` |  |

C++0x include files. These are only available in C++0x compilation mode, i.e. `-std=c++0x` or `-std=gnu++0x`.

Table 3.3. C++ 200x Library Headers

`algorithm` | `iomanip` | `locale` | `regex` | `tuple`
`array` | `ios` | `map` | `set` | `typeinfo`
`bitset` | `iosfwd` | `memory` | `sstream` | `type_traits`
`complex` | `iostream` | `new` | `stack` | `unordered_map`
`deque` | `istream` | `numeric` | `stdexcept` | `unordered_set`
`exception` | `iterator` | `ostream` | `streambuf` | `utility`
`fstream` | `limits` | `queue` | `string` | `valarray`
`functional` | `list` | `random` | `system_error` | `vector`

Table 3.4. C++ 200x Library Headers for C Library Facilities

`cassert` | `cfloat` | `cmath` | `cstddef` | `ctgmath`
`ccomplex` | `cinttypes` | `csetjmp` | `cstdint` | `ctime`
`cctype` | `ciso646` | `csignal` | `cstdio` | `cuchar`
`cerrno` | `climits` | `cstdarg` | `cstdlib` | `cwchar`
`cfenv` | `clocale` | `cstdbool` | `cstring` | `cwctype`

In addition, TR1 includes as:

Table 3.5. C++ TR1 Library Headers

`tr1/array` | `tr1/memory` | `tr1/regex` | `tr1/type_traits` | `tr1/unordered_set`
`tr1/complex` | `tr1/random` | `tr1/tuple` | `tr1/unordered_map` | `tr1/utility`
`tr1/functional` |  |  |  |

Table 3.6. C++ TR1 Library Headers for C Library Facilities

`tr1/cmath` | `tr1/cfloat` | `tr1/cstdarg` | `tr1/cstdio` | `tr1/ctime`
`tr1/ccomplex` | `tr1/cinttypes` | `tr1/cstdbool` | `tr1/cstdlib` | `tr1/cwchar`
`tr1/cfenv` | `tr1/climits` | `tr1/cstdint` | `tr1/ctgmath` | `tr1/cwctype`

Also included are files for the C++ ABI interface:

Table 3.7. C++ ABI Headers

`cxxabi.h` | `cxxabi_forced.h`

And a large variety of extensions.

Table 3.8. Extension Headers

`ext/algorithm` | `ext/debug_allocator.h` | `ext/mt_allocator.h` | `ext/pod_char_traits.h` | `ext/stdio_sync_filebuf.h`
`ext/array_allocator.h` | `ext/enc_filebuf.h` | `ext/new_allocator.h` | `ext/pool_allocator.h` | `ext/throw_allocator.h`
`ext/atomicity.h` | `ext/functional` | `ext/numeric` | `ext/rb_tree` | `ext/typelist.h`
`ext/bitmap_allocator.h` | `ext/iterator` | `ext/numeric_traits.h` | `ext/rope` | `ext/type_traits.h`
`ext/codecvt_specializations.h` | `ext/malloc_allocator.h` | `ext/pb_ds/assoc_container.h` | `ext/slist` | `ext/vstring.h`
`ext/concurrence.h` | `ext/memory` | `ext/pb_ds/priority_queue.h` | `ext/stdio_filebuf.h` |

Table 3.9. Extension Debug Headers

`debug/bitset` | `debug/list` | `debug/set` | `debug/unordered_map` | `debug/vector`
`debug/deque` | `debug/map` | `debug/string` | `debug/unordered_set` |

Table 3.10. Extension Parallel Headers

`parallel/algorithm` | `parallel/numeric`

### Mixing Headers

A few simple rules.

First, mixing different dialects of the standard headers is not possible. It's an all-or-nothing affair. Thus, code like

    #include <array>
    #include <functional>

Implies C++0x mode. To use the entities in <array>, the C++0x compilation mode must be used, which implies the C++0x functionality (and deprecations) in <functional> will be present.

Second, the other headers can be included with either dialect of the standard headers, although features and types specific to C++0x are still only enabled when in C++0x compilation mode. So, to use rvalue references with `__gnu_cxx::vstring`, or to use the debug-mode versions of `std::unordered_map`, one must use the `std=gnu++0x` compiler flag. (Or `std=c++0x`, of course.)

A special case of the second rule is the mixing of TR1 and C++0x facilities. It is possible (although not especially prudent) to include both the TR1 version and the C++0x version of header in the same translation unit:

    #include <tr1/type_traits>
    #include <type_traits>

Several parts of C++0x diverge quite substantially from TR1 predecessors.

### The C Headers and `namespace std`

The standard specifies that if one includes the C-style header (<math.h> in this case), the symbols will be available in the global namespace and perhaps in namespace `std::` (but this is no longer a firm requirement.) One the other hand, including the C++-style header (<cmath>) guarantees that the entities will be found in namespace std and perhaps in the global namespace.

Usage of C++-style headers is recommended, as then C-linkage names can be disambiguated by explicit qualification, such as by `std::abort`. In addition, the C++-style headers can use function overloading to provide a simpler interface to certain families of C-functions. For instance in <cmath>, the function `std::sin` has overloads for all the builtin floating-point types. This means that `std::sin` can be used uniformly, instead of a combination of `std::sinf`, `std::sin`, and `std::sinl`.

### Precompiled Headers

There are three base header files that are provided. They can be used to precompile the standard headers and extensions into binary files that may the be used to speed compiles that use these headers.

- stdc++.h

  Includes all standard headers. Actual content varies depending on language dialect.

- stdtr1c++.h

  Includes all of <stdc++.h>, and adds all the TR1 headers.

- extc++.h

  Includes all of <stdtr1c++.h>, and adds all the Extension headers.

How to construct a .gch file from one of these base header files.

First, find the include directory for the compiler. One way to do this is:

    g++ -v hello.cc

    #include <...> search starts here:
     /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0
    ...
    End of search list.

Then, create a precompiled header file with the same flags that will be used to compile other projects.

    g++ -Winvalid-pch -x c++-header -g -O2 -o ./stdc++.h.gch /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/x86_64-unknown-linux-gnu/bits/stdc++.h

The resulting file will be quite large: the current size is around thirty megabytes.

How to use the resulting file.

    g++ -I. -include stdc++.h  -H -g -O2 hello.cc

Verification that the PCH file is being used is easy:

    g++ -Winvalid-pch -I. -include stdc++.h -H -g -O2 hello.cc -o test.exe
    ! ./stdc++.h.gch
    . /mnt/share/bld/H-x86-gcc.20071201/include/c++/4.3.0/iostream
    . /mnt/share/bld/H-x86-gcc.20071201include/c++/4.3.0/string

The exclamation point to the left of the `stdc++.h.gch` listing means that the generated PCH file was used, and thus the

Detailed information about creating precompiled header files can be found in the GCC documentation.

Prev | Up | Next
Chapter 3. Using | Home | Namespaces