C++ Header order

Konrad picture Konrad · Mar 5, 2009 · Viewed 19.9k times · Source

What order should headers be declared in a header / cpp file? Obviously those that are required by subsequent headers should be earlier and class specific headers should be in cpp scope not header scope, but is there a set order convention / best practice?

Answer

Mykola Golubyev picture Mykola Golubyev · Mar 5, 2009

In a header file you have to include ALL the headers to make it compilable. And don't forget to use forward declarations instead of some headers.

In a source file:

  • corresponded header file
  • necessary project headers
  • 3rd party libraries headers
  • standard libraries headers
  • system headers

In that order you will not miss any of your header files that forgot to include libraries by their own.