Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Can anyone recommend a tool for analyzing and cleaning up excess #include statements throughout a large number of source/header files which aren't actually needed? It would need to be something that could intelligently analyze the files and figure out which #include statements ARE needed to allow removing the excess ones which aren't actually necessary.

The goal is to avoid unnecessary re-compilation triggered by #include statements which aren't actually needed. i.e. you change a header and everything which #include's that header is rebuilt, but a lot of stuff that #include's it doesn't actually need to do so.
Posted
Comments
Sergey Chepurin 9-Sep-11 10:09am    
With PC-Lint the price is the problem (from 389 USD). You can check by yourself Wikipedia's list of tools for code analyzing http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis.

I think that PC-Lint might be able to help with this, I'm pretty sure I've seen it warn about unused headers - I don't have it installed at the moment so can't check.
 
Share this answer
 
Doxygen can be used for that... It can build dependency graphes.

And there a few tricks that can be used to do some cleanup by hand.

1) Select a problematic file and add a #pragma error Is it used in it. Then compile that project. Any file that include that won't compile. Then you can analyse those to see if included are needed or not.

2) Comment out some portion of an header file and see if it still compile. You might be able to split a file so that definition that are changing a lot are not in the same file as complex declaration that seldom changes for example.
 
Share this answer
 
Some time ago I was faced with similar problems and spent two days solving it 'manually' by taking all the #includes out of the header files and inserting them in any cpp file that stopped compiling. Only exceptions were #includes to base classes and a few exceptions that I couldn't easily move because of external dependencies. That alone increased compile times by a factor of 2-3!

Later I've found a product that might have been helpful: IncludeManager. I didn't yet take the time to test it, but since it offers a free trial you might just as well give it a try. I'd expect that it's graphs give you a good idea where optimization might pay off most.
 
Share this answer
 
Rather than dependent on a tool.
i would suggest you to use ur header files under the key word
#pragma once

you may also do as follows:


SampleHeader.h


C++
if !defined SAMPLE_HEADER_H // where SAMPLE_HEADER_H is a alias name for this header declaration
@define SAMPLE_HEADER_H
#endif //SAMPLE_HEADER_H



This will take care ur header file being defined once and not more than that!
 
Share this answer
 
Comments
Legor 13-Sep-11 6:10am    
Easiest and most typical way to do this. My 5
in C++

#pragma once
#ifndef MY_HEADER_
#define MY_HEADER_
some code
#endif
Niklas L 13-Sep-11 9:15am    
This will not reduce dependencies and compile time, which the OP is asking for.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900