Is it a good idea to put all the project headers in a single HEADERS.h file?

I talked with my instructor the other day and asked him this question. He told me that I can go on small projects, but I am starting a chess program, and I was wondering what Qaru was thinking about this problem. Should I include all headers in a single file or separate them?

+4
source share
7 answers

Usually you need separate headers.

Including more than necessary does some potentially bad things.

  • This is the only reason for slow compilation. Unnecessarily including extra headers slows compilation, as each source file must worry about more detailed information than necessary. It starts as a small problem, and before you know, hundreds of developers spend tens and hundreds of hours each, because the problem has gone too far to fix the situation. Despite the fact that you are working on small problems, it is important to understand the correct separation of headings - it is easy to get the right for the first time, but it is very difficult to fix it later if you ignore it.

  • You lose the ability (depending on the compiler / IDE) to correctly handle the dependencies and complete the creation of additional information than you need to build.

  • You lose maintainability. It is harder to maintain software when you have large, single files than when you have many small and compressed files.

  • You make the code more difficult to understand, therefore more error prone. Having everything in one file makes it easier to β€œlose” in a huge header.

+13
source

There are several cases where this practice is useful:

  • Precompiled header files with code that doesn't actually change that often.

  • API

+2
source

This is a bad idea in general, because you may have dependencies that you cannot solve in this case.

But it is usually useful to create a header file with all the basic headers that you use from different libraries or your own header (with descriptions of structures and data types).

0
source

There is no solution for a one-way approach. Arrange your headers as it makes sense. 90 +% of the time, which means that each module includes only those headers that it needs. However, some programs have a small isolation of data structures between modules, which may mean that for

#include "alltheheaders.h" 
0
source

For a smaller project, you will never notice the difference. However, as soon as your project reaches a significant scale, stick to the old adage β€œOnly links in files that directly use them." If you have myclass.cpp and myclass.h, and myclass.h does not directly need a specific header, refer to it in cpp.

This may take longer, but best practice is always worth the time.

0
source

I just want to add this, yes, this is usually a bad idea, but sometimes it can be useful. However, for entire projects.

For example, I recently wrote a utility to dump a stack in windows. There were four general headers that I was going to include, so I made (in the detail folder the agreement I stole the boost form ) a windows.hpp , which included these four. Implementations can then use this header to easily get the necessary functions.

While it may not have the same weight as the mega-inclusions-480 heading, it was a grouping of several common headers, and it was very useful. The key point here is a small collection of related headers used in part of the code.

0
source

You must keep the number of headers included in any source file (* .cpp, * .cc, etc.) to the minimum necessary to compile this file. Including additional headers will increase compilation time. You should also try to reduce the number of inclusions in your headers - you can do this by translating class declarations rather than including their headers.

eg. The following code has an unnecessary inclusion in the header

Example.hh

 #include "SomeClass.hh" void SomeFunction(SomeClass const& obj); 

Example.cc

 #include "Example.hh" void SomeFunction(SomeClass const& obj) { // code here } 

You can write how to move include from the header to the source file

Example.hh

 class SomeClass; void SomeFunction(SomeClass const& obj); 

Example.cc

 #include "Example.hh" #include "SomeClass.hh" void SomeFunction(SomeClass const& obj) { // code here } 
0
source

All Articles