Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why include related header first?

On the question C/C++ include file order/best practices, the best answer recommends to include the related header first.

Same for Google and Mozilla style guides.

However, in both cases, I couldn't find a good reason why you would do this.

Google and Mozilla coding rules look best to me, because they enforce you to include the most "standard" headers first.

This way, I think you are less likely to mess up the included files (for examples by undefining some macros used in the other headers etc.) so that looks like the best way to go for me.

But following that rationale, why would you include the related header first? Since any syntax error in it might mess all the following headers?

I would think including the related header last would be best instead.

like image 342
Arnaud Avatar asked Oct 09 '18 08:10

Arnaud


People also ask

Does order of including header files matter?

Ideally, all header files should be self-contained, and inclusion order should not matter. In practice, people often write header files that are not self-contained, and sometimes inclusion order does matter.

Does order of include files matter?

Does the include order matter? If your header files are self-contained, then the include order technically shouldn't matter at all for the compilation result. That is, unless you have (questionable?) specific design choices for your code, such as necessary macro definitions that are not automatically included.

Should headers include other headers?

Implementation. This rule means that if the header uses a type — such as ' FILE * ' or ' size_t ' - then it must ensure that the appropriate other header ( <stdio. h> or <stddef. h> for example) should be included.

Why header files inclusion is must?

Including Multiple Header Files: You can use various header files in a program. When a header file is included twice within a program, the compiler processes the contents of that header file twice. This leads to an error in the program. To eliminate this error, conditional preprocessor directives are used.


2 Answers

It's to make sure your clients don't hate you when they include your library header.

If the header is brittle and subject to break on wrong inclusion order, it may appear to work in your environment when it isn't first - since you include the headers you need - but fail to compile for client code. Because it may not at all be obvious what other headers need to be pulled in for it to work.

Including first the header which corresponds to the current implementation file goes toward checking that the header is self-contained. Self-containment goes beyond just including the necessary headers. It also entails adding the required forward declarations for types you use in your API. Naturally your header will compile if you include the header for the type before it, but you may not wish to pull it in since you only depend on the type name in your API.

Some style guides prohibit forward declarations, so those may not be part of the rationale they pose.

like image 148
StoryTeller - Unslander Monica Avatar answered Oct 09 '22 04:10

StoryTeller - Unslander Monica


Including the header related to the cpp file first ensures that the header is self contained and doesn't require other includes to compile

like image 27
Alan Birtles Avatar answered Oct 09 '22 04:10

Alan Birtles