How do you maintain the #include statements in your C or C++ project? It seems almost inevitable that eventually the set of include statements in a file is either insufficient (but happens to work because of the current state of the project) or includes stuff that is no longer needed.
Have you created any tools to spot or rectify problems? Any suggestions?
I've been thinking about writing something that compiles each non-header file individually many times, each time removing an #include statement. Continue doing this until a minimal set of includes is achieved.
To verify that header files are including everything they need, I would create a source file that all it does is include a header file and try to compile it. If the compile fails, then the header file itself is missing an include.
Before I create something though, I thought I should ask here. This seems like a somewhat universal problem.
to change one's behavior or character for the better. If you want to land a decent job, you need to clean up your act.
Idiomatic uses of "clean up" include "clean up one's act," and "clean up after." The expression "clean up one's act" means to follow certain standards of behavior or improve the way one behaves; it can only be an action: The teacher told Kim she would have to clean up her act if she expected to pass the class.
Succeed, especially financially, as in We had fantastic luck at the races and really cleaned up. [ Slang; first half of 1800s] 6. Also, clean up on.
When you need a term meaning (1) to make clean or orderly, or (2) to make oneself clean, use clean up—two words. In American and Canadian English, the one-word cleanup is a noun referring to (1) a thorough cleaning or (2) the act or process of cleaning.
To verify that header files are including everything they need, I would creating a source file that all it does is include a header file and try to compile it. If the compile fails, then the header file itself is missing an include.
You get the same effect by making the following rule: that the first header file which foo.c or foo.cpp must include should be the correspondingly-named foo.h. Doing this ensures that foo.h includes whatever it needs to compile.
Furthermore, Lakos' book Large-Scale C++ Software Design (for example) lists many, many techniques for moving implementation details out of a header and into the corresponding CPP file. If you take that to its extreme, using techniques like Cheshire Cat (which hides all implementation details) and Factory (which hides the existence of subclasses) then many headers would be able to stand alone without including other headers, and instead make do with just forward declaration to opaque types instead ... except perhaps for template classes.
In the end, each header file might need to include:
No header files for types which are data members (instead, data members are defined/hidden in the CPP file using the "cheshire cat" a.k.a. "pimpl" technique)
No header files for types which are parameters to or return types from methods (instead, these are predefined types like int
; or, if they're user-defined types, then they're references in which case a forward-declared, opaque type declaration like merely class Foo;
instead of #include "foo.h"
in the header file is sufficient).
What you need then is the header file for:
The superclass, if this is a subclass
Possibly any templated types which are used as method parameters and/or return types: apparently you're supposed to be able to forward-declare template classes too, but some compiler implementations may have a problem with that (though you could also encapsulate any templates e.g. List<X>
as implementation details of a user-defined type e.g. ListX
).
In practice, I might make a "standard.h" which includes all the system files (e.g. STL headers, O/S-specific types and/or any #define
s, etc) that are used by any/all header files in the project, and include that as the first header in every application header file (and tell the compiler to treat this "standard.h" as the 'precompiled header file').
//contents of foo.h #ifndef INC_FOO_H //or #pragma once #define INC_FOO_H #include "standard.h" class Foo { public: //methods ... Foo-specific methods here ... private: //data struct Impl; Impl* m_impl; }; #endif//INC_FOO_H
//contents of foo.cpp #include "foo.h" #include "bar.h" Foo::Foo() { m_impl = new Impl(); } struct Foo::Impl { Bar m_bar; ... etc ... }; ... etc ...
I have the habit of ordering my includes from high abstraction level to low abstraction level. This requires that headers have to be self-sufficient and hidden dependencies are quickly revealed as compiler errors.
For example a class 'Tetris' has a Tetris.h and Tetris.cpp file. The include order for Tetris.cpp would be
#include "Tetris.h" // corresponding header first #include "Block.h" // ..then application level includes #include "Utils/Grid.h" // ..then library dependencies #include <vector> // ..then stl #include <windows.h> // ..then system includes
And now I realize this doesn't really answer your question since this system does not really help to clean up unneeded includes. Ah well..
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With