#include Guidelines
This is mostly for my own reference, but here are some of the guidelines I like to follow when it comes to #include
s in C++. Given foo.cpp
and foo.h
:
-
foo.h
should include everything it mentions and no more. For example, if you useboost::random
in the implementation, don't#include <boost/random.hpp>
infoo.h
. -
foo.cpp
should 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.cpp
only 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
#include
s 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
#include
s if at all possible. -
Forward declare whenever you can get away with it.