Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

include

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 find that as I add additional classes and more class inter-dependencies, I have to #include other header files. After a week or two, I end up with #include directives in lots of places. Later, I'll try removing some of the #includes and discover that everything still works because some OTHER included class is also #including what I just removed.

Is there a simple, easy rule for putting in #includes that will stop this ugly mess from happening in the first place? What is the best practice?

For example, I've worked on projects where the Implementation .cpp file ONLY includes the corresponding Definition .hpp file, and nothing else. If there are any other .hpp files that need to be used by the Implementation .cpp, they are all referenced by the Definition .hpp file.

like image 205
Runcible Avatar asked Mar 27 '09 19:03

Runcible


People also ask

What is considered a standard?

A standard is a repeatable, harmonised, agreed and documented way of doing something. Standards contain technical specifications or other precise criteria designed to be used consistently as a rule, guideline, or definition.

What is an example of standard?

The definition of a standard is something established as a rule, example or basis of comparison. An example of standard is a guideline governing what students must learn in the 7th grade. An example of standard is a piece of music that continues to be played throughout the years.

What's your standard meaning?

1 : something established by authority, custom, or general consent as a model, example, or point of reference the standard of the reasonable person. 2 : something established by authority as a rule for the measure of quantity, weight, extent, value, or quality. 3 : the basis of value in a monetary system.

What does set the standard mean?

to perform an activity at a level that other people have to try to achieve. a company that sets the standard in overnight delivery. Synonyms and related words. To do something well or better than someone else. excel.


1 Answers

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.
like image 152
Antti Huima Avatar answered Oct 06 '22 00:10

Antti Huima