0

sorry, I'm an absolute rookie, I know my question is ignorant.

but I wonder why every time we need to use different #include directives to enable different supports from C++ library ??

Why not just let us use all supports from C++ library from the beginning?

daniel2026
  • 11
  • 1
  • it's a way to avoid bloat and keep things simple. You don't want to use a cannon to kill a mosquito – FCR Apr 11 '21 at 10:22
  • 3
    Because each single one of them slightly slows down the compilation. – HolyBlackCat Apr 11 '21 at 10:25
  • 1
    This is explained in detail here: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h (`#include ` is basically including everything) – fabian Apr 11 '21 at 10:30
  • Assuming you meant to put the word standard between C++ and library, one of the foundational criteria of the language is this: you don't pay for what you don't use. – MatG Apr 11 '21 at 10:35
  • [Why do we have to include multiple header files for a single library (the C standard library)?](https://softwareengineering.stackexchange.com/q/357316/313845) – Aykhan Hagverdili Apr 11 '21 at 10:35
  • compilation time of `#include int main(){}` is three seconds, and its just only one of many reasons. – zkoza Apr 11 '21 at 10:37
  • Thank you all, a lot of useful explanations :) – daniel2026 Apr 11 '21 at 10:39
  • @daniel2026, all information that you could have found if you searched. – Enlico Apr 11 '21 at 12:55

1 Answers1

1

Separating things into multiple header files keeps things simpler for the compiler and linker. The more code they have to process, the longer the compilation process will take. You don't want them wasting efforts on code you are not interested in.

Also, using multiple header files is good for organization, keeping related functionality grouped together, allowing you more flexibility to cherry-pick only the functionality you actually want to use.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770