HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppCritical

C/C++ include header file order

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
includeheaderfileorder

Problem

What order should include files be specified, i.e. what are the reasons for including one header before another?

For example, do the system files, STL, and Boost go before or after the local include files?

Solution

I don't think there's a recommended order, as long as it compiles! What's annoying is when some headers require other headers to be included first... That's a problem with the headers themselves, not with the order of includes.

My personal preference is to go from local to global, each subsection in alphabetical order, i.e.:

  • h file corresponding to this cpp file (if applicable)



  • headers from the same component,



  • headers from other components,



  • system headers.



My rationale for 1. is that it should prove that each header (for which there is a cpp) can be #included without prerequisites (technically speaking: header is "self-contained"). And the rest just seems to flow logically from there.

Context

Stack Overflow Q#2762568, score: 424

Revisions (0)

No revisions yet.