-2

I understand that the purpose of having header files in C++ is to separate the interface from the implementation. In other words, we're specifying a contract between the implementation file and header file, that it must write to the specifications of the function signatures inside the header file and if it doesn't do so, a compiler error is thrown.

However, I don't understand whether this makes compilation faster or just decreases development time? It makes sense to have forward declarations of our functions and have the compiler catch any errors we make in our definitions (for example, we have a different number of parameters in the implementation file's function than what was specified in the header). So catching these errors at compile time rather than run time definitely helps with development time.

At a lower-level, is there any advantage in compile time by including our own header files?

Edit: OK, if there is no advantage in compilation time, why do we have header files besides the separation of interface and implementation?

Mutating Algorithm
  • 2,604
  • 2
  • 29
  • 66
  • 2
    There are many reasons and separation of interface/implementation is usually not the most prominent. And no, header files can *slow down* compilation. For example, using the GCC C++ compiler `g++` and the most basic "hello world" program, compare the time it takes if you only include the least minimal header files required (i.e. only ``) and including just about *all* standard header files with the implementation-specific `` (which you [should never include](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)). – Some programmer dude Jul 27 '18 at 17:18
  • ***is there any advantage in compile time by including our own header files?*** There is a disadvantage to including header files that you don't need. – drescherjm Jul 27 '18 at 17:19
  • 3
    As for the biggest reason for header files, IMO: Lets say you have a class definition that needs to be known in multiple source files. Would you rather rewrite or copy-paste the whole class definition into all the source files? Or write it once in a single header file that you can easily include? – Some programmer dude Jul 27 '18 at 17:20
  • Using a precompiled header might make things faster for large projects, but its creation is at a significant cost, too. – AlexG Jul 27 '18 at 17:20
  • 1
    ***why do we have header files besides the separation of interface and implementation?*** @Someprogrammerdude gave one reason. – drescherjm Jul 27 '18 at 17:23
  • 2
    They don't - it's slower compilation, faster linking. –  Jul 27 '18 at 17:23
  • @Someprogrammerdude Ah, I see. – Mutating Algorithm Jul 27 '18 at 17:24
  • Faster than what? – Galik Jul 27 '18 at 17:26
  • 1
    There is a modules proposal (n4720) that may make C++20, which is an alternative to using header files. If the proposal works out, it ought to be a game changer. But we'll have a long transition with both includes and modules, and headers will continue to be supported for the foreseeable future for a very long time. (Modules will not make the C Preprocessor go away.) – Eljay Jul 27 '18 at 17:45
  • 1
    Yes, proper use of header files allows to drastically cut compilation times. But this is mostly achieved by eliminating multiple transition units. This technique is known as SCU / unity build / everything-is-header-only. – user7860670 Jul 27 '18 at 20:00

1 Answers1

0

Consider we just have implementation files-no header file. Then where ever we have to use particular class/function we will have to include complete implementation to that file, that will eventually end up compiling the complete implementation multiple time that will eventually increase the compile time.

But when we just include declarations we just compile declarations multiple times that is less costly.

Also it must be kept in mind that we should include even header files only where it is actually required. Not ever where.

Other advantage of having header files: In case of dlls/libs, where we have compiled implementations, header files are good source of insight information about the details of the classes/functions available in the libs.

Rizwan
  • 3,324
  • 3
  • 17
  • 38