Is there a standard #include convention for C++?

前端 未结 8 1902
隐瞒了意图╮
隐瞒了意图╮ 2021-01-01 16:49

This is a rather basic question, but it\'s one that\'s bugged me for awhile.

My project has a bunch of .cpp (Implementation) and .hpp (Definition) files.

I f

8条回答
  •  春和景丽
    2021-01-01 17:22

    Some best practices:

    • Every .cpp or .C file includes all headers it needs and does not rely on headers including other related headers
    • Every .hpp or .h file includes all its dependencies and does not rely on the included headers including other related headers
    • Every header is wrapped with:

      #ifndef HEADER_XXX_INCLUDED
      #define HEADER_XXX_INCLUDED
      ...
      #endif /* HEADER_XXX_INCLUDED */
      
    • Headers do not include each others in cycles

    • Often: there is a single "project-wide header file" like "config.h" or ".h" which is always included first by any .cpp or .C file. Typically this has platform related configuration data, project-wide constants and macros etc.

    These are not necessarily "best practice", but rules which I usually follow also:

    • Project-specific headers are included as #include "..." and before the system-wide headers, which are included as #include <...>
    • Project-specific headers are included in alphabetical order as a way to ensure that there is no accidental, hidden requirement on which order they are included. As every header should include its dependents and the headers should be protected against multiple inclusion, you should be able to include them in any order you wish.

提交回复
热议问题