Click to See Complete Forum and Search --> : Get rid of #include that is not used


jnteoh
July 3rd, 2002, 11:26 AM
Most often during development, we "#include" something that is not use later on. However, these "#include" statements are still somewhere in the source files.

For instance, I include #include "Apple.h" because I need Apple class, but later decide not to use Apple in my source. Now that include statement is hanging somewhere in the source which I should be get rid of. Because it creates dependency during compilation.

Is there an easy way to detect which #include statement that is not being used?

Thanks.:)

Paul McKenzie
July 3rd, 2002, 11:44 AM
It's not really that easy to do, unless your compiler has an option to detect unused #include files.

I know that the PC-LINT program from www.gimple.com can detect unused #include files. Maybe there are other utilities out there that can do this. But trying to do this by hand, like I said, is not easy, and most likely error-prone.

Regards,

Paul McKenzie

Anthony Mai
July 3rd, 2002, 03:50 PM
It's not really that hard to do, unless you have hundreds of include files. Just comment out your #include statements one by one, working from the bottom upwards, and try to see if it compiles. If it can compile without a problem, the commented out include is not needed.

Except you have to watch out for those include files that provides #define's that are compilation directives, for example
#define USE_WIN32
or something like that. Removing such include file may not cause any compilation problem, but will cause the wrong code to be compiled.