tags:

views:

140

answers:

4

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

Thanks in advance

A: 

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

Ram Bhat
I am trying to find out redundant header files and not the list of all the included header files
Ravi Gupta
Unclear how this determines if you needed other symbols from this file?
Nick Bastin
+1  A: 

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
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
@Jens Gustedt: Can you plz explain it little more i.e how to use them? or provide any tutorial link for this.
Ravi Gupta
You find the documentation of doxygen here:http://www.stack.nl/~dimitri/doxygen/ If you want it to also producenice call/include/whatever graphs you also have to installgraphviz. Both should be available in any recent linux distro. (Noidea for windows...) Doxygen has a config file that you should editto turn the feature of include graphs on. All of this is quite welldocumented is the default file they provide. Usually you wouldannotate your code for doxygen to produce a documentation, but foryour purpose sources just as they are should be fine, too.
Jens Gustedt
+1  A: 

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
Getting rid of redundant/superfluous includes can speed up compile times. For large systems this can become quite meaningful.
Craig W. Wright
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
@Craig W. Wright: That's why I am looking for it.
Ravi Gupta
@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
@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
@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
+2  A: 

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