I need a tool that will scan my C++ project to see if there are any includes that are not being referenced or are being referenced redundantly. Thanks.
-
1Are you using include guards? e.g. `#ifndef __YOUR_HEADER_H__` \n `#define __YOUR_HEADER_H__`\n `// your code...` \n `#endif` – RageD Jan 25 '11 at 23:42
-
It's easier to check which are really needed :) – ruslik Jan 25 '11 at 23:49
-
Now Christopher's narrowed his interest to unneeded includes in light of Noah's answer, this simplifies to http://stackoverflow.com/questions/614794 – Tony Delroy Jan 26 '11 at 01:31
-
possible duplicate of [C/C++: Detecting superfluous #includes?](http://stackoverflow.com/questions/614794/c-c-detecting-superfluous-includes) – Tony Delroy Jan 26 '11 at 01:31
2 Answers
You don't want this. You want to include any header that declares/defines anything used by the cpp file you're writing. If you remove "redundant" headers that are already included by something you're including then when something minor changes you'll be editing files all over the damn place. Just use proper header guards to make sure you don't break the one-definition rule.

- 40,307
- 7
- 73
- 125
-
2+1 from me. I can see a point for his question in the case that includes are not being used, though. – San Jacinto Jan 25 '11 at 23:48
-
2
-
2In conclusion, after reading these recommendations, I agree with San Jacinto. I could use a tool that would check for 'unreferenced includes' rather than redundant includes because #pragma prevents redundancy. – Christopher Peterson Jan 26 '11 at 00:24
-
Beware of pragma. It's path based, not define based. I've spent more than just one hour trying to figure out why my header was being included twice even though it had a pragma telling it not to. Since that day I've never used it again. – Edward Strange Jan 26 '11 at 01:20
As for the tool - hard to imagine how should it work (at least from my limited point of view).
What I do is commenting out each include line and rebuilding the file (only one file - not the complete project). If it stil compiles - the inclusion was not needed.
Should not take too much time.
Off-topic: I can see the need of this procedure before delivering the code to the customer. I would appreciate the deliverables more knowing that somebody has taken care even of that tiny detail. But as a customer, I wouldn't be so picky to insist on it.

- 194
- 2
- 15