c++includeheader-filescode-organization

In what order should headers be included?


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?


Solution

  • 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.