4

Is there any way to find out all the redundant header files included in a C/C++ source file?

BenMorel
  • 30,280
  • 40
  • 163
  • 285
Ravi Gupta
  • 5,684
  • 16
  • 52
  • 76
  • Try out the answers to this question: http://stackoverflow.com/questions/42308/tool-to-track-include-dependencies – In silico Jun 19 '10 at 06:36
  • 3
    @In silico: that's the way to find *all* dependencies. I think the OP is asking about *redundant* #inclusions. I find them by removing #includes and then trying to compile. I know of no tool that does it automatically, which is strange for such a simple and repetitive task. – Beta Jun 19 '10 at 06:46
  • Redundant as in unnecessary or as in included multiple times? – R Samuel Klatchko Jun 19 '10 at 06:48
  • What does redundant mean? If you're including multiple times, you're just failing at include guards - otherwise it isn't costing you anything because the guard will prevent the inclusion. If you looking for unnecessary filtering, look at http://stackoverflow.com/questions/1301850/tools-to-find-included-headers-which-are-unused – Nick Bastin Jun 19 '10 at 06:57
  • @R Samuel Klatchko: unnecessary includes – Ravi Gupta Jun 19 '10 at 06:57
  • Duplicate of http://stackoverflow.com/questions/74326/how-should-i-detect-unnecessary-include-files-in-a-large-c-project – KeithB Jun 19 '10 at 14:34
  • Possible duplicate of [C/C++: Detecting superfluous #includes?](https://stackoverflow.com/questions/614794/c-c-detecting-superfluous-includes) – phuclv Oct 09 '17 at 02:33
  • Possible duplicate of [Tools to find included headers which are unused?](https://stackoverflow.com/questions/1301850/tools-to-find-included-headers-which-are-unused) – phuclv Nov 28 '17 at 08:39

4 Answers4

5

Be aware that redundant includes may be a good thing here, because it provides self-containment of header files. I.e. if B includes A, and C includes both B and A:

headera.h

headerb.h
#include "headera.h"

headerc.h
#include "headerb.h"
#include "headera.h"

you could argue that the inclusion of A is redundant in C, since it is already provided by the inclusion of B. But in fact it makes C independent from the inner structure of B. Removing it would make C dependent on B to include A.

Secure
  • 4,030
  • 1
  • 14
  • 15
  • but i am looking for #include which are not at all required by the program, for e.g - If C.c includes, `limits.h` and C.c doesn't actually uses anything defined in it. – Ravi Gupta Jun 19 '10 at 17:20
2

I use doxygen (together with graphviz) to get the include graph. Then the `redundant' includes are the transitive arcs, i.e arcs that introduce a short cut on a longer path.

Jens Gustedt
  • 72,200
  • 3
  • 92
  • 164
  • 2
    Not in general: if header A includes header B, and if it's not documented that header A includes header B, then you still have to include header B to use something from header B. – Philipp Jun 19 '10 at 09:16
  • Can you plz explain it little more i.e how to use them? or provide any tutorial link for this. – Ravi Gupta Jun 19 '10 at 17:24
  • You find the documentation of doxygen here: http://www.stack.nl/~dimitri/doxygen/ If you want it to also produce nice call/include/whatever graphs you also have to install graphviz. Both should be available in any recent linux distro. (No idea for windows...) Doxygen has a config file that you should edit to turn the feature of include graphs on. All of this is quite well documented is the default file they provide. Usually you would annotate your code for doxygen to produce a documentation, but for your purpose sources just as they are should be fine, too. – Jens Gustedt Jun 19 '10 at 18:14
1

This is kind of a complex question. It can be interpreted one of two ways:

  1. You want to remove #includes that don't provide you anything.
  2. You want to look for recursive includes.

1 probably isn't required. Includes just provide information for the compiler, they shouldn't have allocation in them. Even if they do and you don't do it, the compiler will dead-strip this. If you really want to do this, you can start removing includes you don't think you need until you get "implicit declaration of..." errors.

For 2, you usually don't have to worry. It's pretty common practice to use a unique #def i.e.:

#ifndef __MY_LIB_H
#define __MY_LIB_H
...
#endif

This will cause the library guts to be omited if the definition is already present.

If you control all or most of the libs you could change the #ifndef to:

#ifdef __MY_LIB_H
#error "Lib included recursively"
#else
...
#endif
Jon L
  • 273
  • 1
  • 7
  • Getting rid of redundant/superfluous includes can speed up compile times. For large systems this can become quite meaningful. – Craig Wright Jun 19 '10 at 15:27
  • 2
    Don't use two leading underscores (or an underscore followed by a capital letter) when naming your `#include`-guards. Those names are reserved for the compiler. – jamesdlin Jun 19 '10 at 15:56
  • @Jon L: I am looking for something to handle case one i.e `You want to remove #includes that don't provide you anything.` – Ravi Gupta Jun 19 '10 at 17:22
  • @jamesdlin I had a discussion about exactly that sometime after writing this. I usually only use '_' I'll be more careful though, thanks. – Jon L Jun 23 '10 at 06:11
  • @Ravi I was talking more in the general case. Though arguable you should be able to inspect for includes you don't need. If you have lots of code that's going to be a real problem. Hmmm... I'm sure there is a tool that would be able to do it though. Basically you want to "cscope" all the none local functions and then generate the unique list. – Jon L Jun 23 '10 at 06:13
0

you can also use #ifdef to cheak for it inside a program. For this your header will need to have some distinct variable. If it exists its defined..

Laz
  • 5,236
  • 9
  • 37
  • 53