Skip to content

Latest commit

 

History

History
99 lines (77 loc) · 6.63 KB

CppInclude.md

File metadata and controls

99 lines (77 loc) · 6.63 KB

#include is a preprocessor directive to add a header file (.h or .hpp) or other files to your program.

There are multiple forms of a header file #includes:

#include <iostream> // 1: STL
#include <myassert.hpp> // 2: The standard header file directory
#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.

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

#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

Conside not #including these [8], but add these to your project instead. For example, in C++ Builder, select 'Project | Add to Project'.

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

  • [1] Herb Sutter. Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include unnecessary header files'.
  • [2] 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'.
  • [3] Herb Sutter. Exceptional C++. 2000. ISBN: 0-201-61562-2. Item 26: 'Never #include a header when a forward declaration will suffice'.
  • [4] 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.'
  • [5] Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter C.14.15: 'Don't pollute the global namespace'
  • [6] Bjarne Stroustrup. The C++ Programming Language (3rd edition). 1997. ISBN: 0-201-88954-4. Chapter 8.2.9.1: 'Namespaces and C'.
  • [7] John Lakos. Large-Scale C++ Software Design. 1996. ISBN: 0-201-63362-0. Section 3.2, page 110: 'The .c file of every component should include its own .h file as the first substantive line of code'
  • [8] Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 32: 'The #include pre-processor directive will only be used to include header (*.h) files.'
  • [9] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[5] Use #include only at global scope and in namespaces'
  • [10] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 15.5. Advice. page 444: '[6] #include only complete declarations'
  • [11] Paul Deitel, Harvey Deitel. C++11 for programmers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 2.2, Common Programming Error 2.1. page 21: 'Forgetting to include the <iostream> header in a program that inputs data from the keyboard or outputs data to the screen causes the compiler to issue an error message'
  • [12] Paul Deitel, Harvey Deitel. C++11 for programmers (2nd edition). 2014. ISBN: 978-0-13-343985-4. Chapter 3.6, Error-Prevention Tip 3.3. page 57: 'To ensure that the preprocessor can locate headers correctly, #include preprocessing directives should place user-defined headers names in quotes [...] and place C++ Standard Library headers names in angle brackets [...]'
  • [13] Jason Turner, cppbestpractices: Use quotes for Including Local Files