Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

(C++) #include

 

A preprocessor statement to add a header file (.h or .hpp) or other files to your program.

 

There are three different header file #includes:

 

1) #include <iostream> // 1: STL

2) #include <myassert.hpp> // 2: The standard header file directory

3) #include "MyUnit.h" // 3: Local

 

The first, without the .h extension, means that this header file is from the Standard Template Library. The second #include means that the header is not from the STL but in the standard header file directory. The third #include means that the file is local, that is in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.

 

*        Never #include unnecessary header files [0]

*        Prefer to #include <iosfwd> when a forward declaration of a stream will suffice [1]

*        Never #include a header when a forward declaration will suffice [2]

 

The C style include on STL headers

 

For backwards compatibility with C one can #include C++ header files with the .h extension. Do not do this: call the correct C++ header file. For a list of all C++ standard header files, go to the header file page.

 

 

#include <stdio.h> //C-stle #include, avoid to do this

 

 

The header file 'stdio.h' is a wrapper: all it does is call the C++ header file cstdio and then adds a 'using namespace std', as C does not have namespaces.

 

This has the unfortunate side-effect that after calling such a header file all functions and classes in namespace std will be in the global namespace.

 

As it pollutes the global namespace, avoid using namespace std [4] [5] .

 

An example from [5] :

 

 

#include <vector> //Carefully avoids polluting the global namespace

 

vector v1; //Error: no 'vector' in global namespace

 

#include <stdio.h> //Contains a 'using namespace std'.

 

vector v2; //Oops: this now works

 

 

#including implementation (.c or .cpp) files

 

These normally are not #included. Instead, these are added to your project. For example, in C++ Builder, select 'Project | Add to Project'.

 

Forgetting to add an implementation file to you project results in a link error.

 

References

 

[0]   Herb Sutter . Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include unnecessary header files'.

[1]   Herb Sutter . Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Prefer to #include <iosfwd> when a forward declaration of a stream will suffice'.

[2]   Herb Sutter . Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include a header when a forward declaration will suffice'.

[3]   C++ FAQ Lite: http://www.parashift.com/c++-faq-lite/coding-standards.html#faq-27.5. Item 27.5: 'Should I use using namespace std in my code? Probably not.'

[4]   Bjarne Stroustrup . The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'

[5]   Bjarne Stroustrup . The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 8.2.9.1: 'Namespaces and C'.

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.