Is there any way to find out all the redundant header files included in a C/C++ source file?
Thanks in advance
Is there any way to find out all the redundant header files included in a C/C++ source file?
Thanks in advance
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..
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.
This is kind of a complex question. It can be interpreted one of two ways:
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
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.