167

I have read from a codeforces blog that if we add #include <bits/stdc++.h> in a C++ program then there is no need to include any other header files. How does #include <bits/stdc++.h> work and is it ok to use it instead of including individual header files?

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
  • 5
    Most likely this is for educational purposes. I assume that `bits/stdc++.h` includes all C++ headers. – 101010 Aug 14 '14 at 14:48
  • `/agree` with the above. Why would have the standard committee bothered with separating functionalities of the STL into multiple headers in the first place if including just one header had been faster, portable, easy and better? – Marco A. Aug 14 '14 at 14:55
  • 5
    From the header source itself: *This is an implementation file for a precompiled header.* – Yuushi Aug 14 '14 at 14:57
  • 3
    @MarcoA. Because computers in 1980 had less memory. – Neil Kirk Aug 14 '14 at 14:57
  • 70
    @NeilKirk: I used that tool you mentioned, followed the first link, arrived to this page, then saw your comment, and got stuck in a loop. – riv Oct 28 '14 at 22:29
  • 3
    There was an effort way back by none other than BS to essentially standardize a kitchen sink header for new users and for quick toy builds. It died somehow (bad practice for large real projects? I also remember they wanted release and debug to have the same ABI which was unworkable). I can't find the reference paper though. I think there is a legitimate niche for such a header though. Although this should all magically go away when we get modules. – emsr Aug 04 '15 at 19:35

3 Answers3

166

It is basically a header file that also includes every standard library and STL include file. The only purpose I can see for it would be for testing and education.

Se e.g. GCC 4.8.0 /bits/stdc++.h source.

Using it would include a lot of unnecessary stuff and increases compilation time.

Edit: As Neil says, it's an implementation for precompiled headers. If you set it up for precompilation correctly it could, in fact, speed up compilation time depending on your project. (https://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html)

I would, however, suggest that you take time to learn about each of the sl/stl headers and include them separately instead, and not use "super headers" except for precompilation purposes.

bscharan
  • 137
  • 1
  • 13
Zelix
  • 1,914
  • 1
  • 14
  • 11
  • 22
    It's for precompiled headers, which decrease compile time! (When set up right) – Neil Kirk Aug 14 '14 at 14:56
  • 11
    I don't think it's very good to learn C++ totally unaware of where things you're using are defined.. at what point do you stop and figure it out? – OJFord Aug 14 '14 at 14:59
  • 32
    @OllieFord Never, ideally. This is 21st century. Compilers should figure out what parts of the standard library I need, to save me time to code important stuff. But of course people should learn about the headers, as it is part of the language. – Neil Kirk Aug 14 '14 at 15:03
  • 1
    No, but depending on student age and curriculum and so forth ... – Zelix Aug 14 '14 at 15:03
  • 2
    A slight disadvantage of including it is that it results in having many names in the namespace. On rare occasions this could lead to some hard-to-understand bugs (for a few seconds). For example if one of your variables is named `count`. But I can't think of a specific confusing error example at the moment... – Evgeni Sergeev Apr 05 '16 at 07:45
  • 1
    The main purpose for this header file would be programming competitions. I would recommend avoidiing it in any serious contexts. – Apollys supports Monica Aug 15 '18 at 23:05
  • It's also pretty useful in Competitive Programming :D – The Peeps191 Jul 22 '22 at 15:24
  • @ThePeeps191 useless you mean. Because usefulness in a useless game is debatable. Also,competitive programming often measures running time\memory used. `stdc++.h` includes all component headers, resulting in static initialization of many object you would never use, there are singletons and global variables under the hood of some subsystems. If anything it's a hack for bad memory of what is defined where – Swift - Friday Pie Jul 09 '23 at 08:36
36

That header file is not part of the C++ standard, is therefore non-portable, and should be avoided.

Moreover, even if there were some catch-all header in the standard, you would want to avoid it in lieu of specific headers, since the compiler has to actually read in and parse every included header (including recursively included headers) every single time that translation unit is compiled.

  • 15
    That's what precompiled headers are for. I have one with pretty much the whole standard library in it. That's just how roll. – Neil Kirk Aug 14 '14 at 14:51
  • 2
    @NeilKirk: Sounds non-standard to me. – Robert Allan Hennigan Leahy Aug 14 '14 at 14:53
  • 5
    No it's not. It's an option on most compiler to speed it up. – Neil Kirk Aug 14 '14 at 14:54
  • 10
    By definition "most compilers" is non standard - if it were standard then it would be "all compilers" I don't see anywhere in the standard where it talks about how headers are precompiled... – Jerry Jeremiah Jan 24 '17 at 23:57
  • If we avoided everything that wasn't part of the standard, then we would rewrite everything for every project. At some point using other people's libraries is a good idea even if they aren't part of the standard. – Jerry Jeremiah Dec 16 '20 at 20:58
5

Unfortunately that approach is not portable C++ (so far).

All standard names are in namespace std and moreover you cannot know which names are NOT defined by including and header (in other words it's perfectly legal for an implementation to declare the name std::string directly or indirectly when using #include <vector>).

Despite this however you are required by the language to know and tell the compiler which standard header includes which part of the standard library. This is a source of portability bugs because if you forget for example #include <map> but use std::map it's possible that the program compiles anyway silently and without warnings on a specific version of a specific compiler, and you may get errors only later when porting to another compiler or version.

In my opinion there are no valid technical excuses that explain why this is necessary for the general user: the compiler binary could have all standard namespace built in and this could actually increase the performance even more than precompiled headers (e.g. using perfect hashing for lookups, removing standard headers parsing or loading/demarshalling and so on).

The use of standard headers simplifies the life of who builds compilers or standard libraries and that's all. It's not something to help users.

However this is the way the language is defined and you need to know which header defines which names so plan for some extra neurons to be burnt in pointless configurations to remember that (or try to find and IDE that automatically adds the standard headers you use and removes the ones you don't... a reasonable alternative).

6502
  • 112,025
  • 15
  • 165
  • 265