I'm looking for a tool to help detect unnecessary header includes in a large c++ code base. The other stackoverflow questions on this topic all suggest cppclean. So I've installed cppclean and I'm trying to use it but even on trivially wrong examples it doesn't give any results.
For example, here's what I'm trying to clean. The source file:
// foo.cpp
#include "bar.h"
void main() { };
And the header file:
// bar.h
class bar {
};
And I run:
cppclean foo.cpp
But it prints nothing and returns 0.
Am I doing something wrong? Are there any tutorials anywhere on how to use this tool?
In the C and C++ programming languages, a header file is a file whose text may be automatically included in another source file by the C preprocessor by the use of a preprocessor directive in the source file. Header files can sometimes contain very large amounts of source code (for instance, the header files windows.
"Include what you use" means this: for every symbol (type, function variable, or macro) that you use in foo.cc, either foo.cc or foo. h should #include a . h file that exports the declaration of that symbol.
Header files are used in C++ so that you don't have to write the code for every single thing. It helps to reduce the complexity and number of lines of code. It also gives you the benefit of reusing the functions that are declared in header files to different .
A TL;DR definition: A header file must include the header files that directly define each of the types directly used in or that directly declare each of the functions used in the header file in question, but must not include anything else.
cppclean got updated in the meantime (June 2019) and includes now:
Classes with virtual methods, no virtual destructor, and no bases
Global/static data that are potential problems when using threads
Functions that are declared but not defined
Unnecessary forward class declarations
Unnecessary function declarations
Undeclared function definitions
Unnecessary #includes in header files
Still not available :
(planned) Unnecessary #includes in source files
(planned) Source files that reference headers not directly #included, ie, files that rely on a transitive #include from another header
(planned) Unused members (private, protected, & public) methods and data
(planned) using namespace std in header files
(planned) Methods that are declared but not defined
As a conclusion :
cppclean .
should do the job now or alternatively :
cppclean <path>
Further Information and Source
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