Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organize includes

Tags:

  • Is there some preferred way to organize ones include directives?
  • Is it better to include the files you need in the .cpp file instead of the .h file? Are the translation units affected somehow?
  • How about if I need it in both the .h file and .cpp file, should I just include it in the .h file? Will it matter?
  • Is it a good practice to keep the already defined files in a precompiled header (stdafx.h), for instance std and third party libraries? How about my own files, should I include them in a stdafx.h file along the way as I create them?

// myClass.h #include <string> // ^-------- should I include it here? --------  class myClass{     myClass();     ~myClass();      int calculation() };  // myClass.cpp #include "myClass.h" #include <string> //  ^-------- or maybe here?  --------  [..]  int myClass::calculation(){     std::string someString = "Hello World";     return someString.length(); }   // stdafx.h #include <string.h> // ^--------- or perhaps here, and then include stdafx.h everywhere? ------- 
like image 358
default Avatar asked Jun 21 '11 11:06

default


People also ask

Why we use #include in C++?

Tells the preprocessor to include the contents of a specified file at the point where the directive appears.

What is the# include directive in c++?

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program.

Where does #include look for files?

GCC looks for headers requested with #include " file " first in the directory containing the current file, then in the directories as specified by -iquote options, then in the same places it would have looked for a header requested with angle brackets.


1 Answers

  1. You should have them at the top of the file, all in one place. This is what everyone expects. Also, it is useful to have them grouped, e.g. first all standard headers, then 3rd-party headers (grouped by library), then your own headers. Keep this order consistent throughout the project. It makes it easier to understand dependencies. As @James Kanze points out, it is also useful to put the header that declares the content first. This way you make sure that it works if included first (meaning it does no depend on any includes that it does not include itself).
  2. Keep the scope as small as possible, so that a change in the header affects the least number of translation-units. This means, whenever possible include it in the cpp-file only. As @Pedro d'Aquino commented, you can reduce the number of includes in a header by using forward declarations whenever possible (basically whenever you only use references or pointers to a given type).
  3. Both - explicit is better than implicit.
  4. After some reading, I believe you should only include headers in the PCH if you are confident that they do not change anymore. This goes for all standard headers as well as (probably) third party libraries. For your own libraries, you be the judge.
like image 105
Björn Pollex Avatar answered Oct 07 '22 17:10

Björn Pollex