Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use cppclean to find unused headers

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?

like image 578
Shum Avatar asked Dec 08 '14 01:12

Shum


People also ask

Are header files automatically included?

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.

How do you use include what you use?

"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.

Where are header files used?

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 .

Can you include header files in header files?

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.


1 Answers

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

    • No direct reference to anything in the header Header is unnecessary if classes were forward declared instead
    • Inconsistent case in #includes (foo.h versus Foo.h)

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

like image 130
Denise P Avatar answered Oct 18 '22 20:10

Denise P