开发者

Checking for redundant included header files

Is there any way to find ou开发者_如何学JAVAt all the redundant header files included in a C/C++ source file?


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.


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜