CppInclude

(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 <assert.hpp> // 2: The standard header file directory
  3. include "Unit1.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, i.e. in the same directory as the program. If the local #include fails, the standard header file directory is checked for this header file.

The C style include on  ?CppStl STL headers

For backwards compatibility with C one can [[CppInclude
    1. include]] C++ header files with the .h extension.
  1. include <stdio.h> //C-stle #include !!!


This header file 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 [6]:

  1. include <vector> //Carefully avoids polluting the global namespace
vector v1; //Error: no 'vector' in global namespace
  1. include <stdio.h> //Contains a 'using namespace std'.
vector v2; //Oops: this now works


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 linking error.

'#include' links

References



last edited (December 3, 2006) by bilderbikkel

Back to the page