#include Guidelines
This is mostly for my own reference, but here are some of the guidelines I like to follow when it comes to #includes in C++. Given foo.cpp and foo.h:
-
foo.hshould include everything it mentions and no more. For example, if you useboost::randomin the implementation, don't#include <boost/random.hpp>infoo.h. -
foo.cppshould also include everything it mentions and no more. -
foo.cpp's first include should be#include "foo.h". -
Precompiled headers, if used, should be specified in
foo.cpponly and will need to be the first#include(#include "foo.h"will then be the second). -
Except for the above two exceptions, the order I like to follow for
#includes is:- Headers from your own project (generally of the form
#include "baz.h") - Headers from non-standard libraries (such as Qt)
- Standard libraries (I include Boost in here because I use it so pervasively)
- Platform headers (like
#include <windows.h>)
- Headers from your own project (generally of the form
-
Within each of the above groups try to alphabetically order the
#includes if at all possible. -
Forward declare whenever you can get away with it.
