Check unnecessary Header file inclusion

P

Parag Mahajan

Hi,

I would like to know if there is any way / tool available by which we
can detect unnecessary header files included in the source files.

Regards,
Parag.
 
I

Ian Collins

Parag said:
Hi,

I would like to know if there is any way / tool available by which we
can detect unnecessary header files included in the source files.
Why bother?
 
P

Parag Mahajan

Hardly, unless your build system is grossly underspecified.

One reason is to reduce the build time and another one is to actually
make sure we do have only required header inclusions in our source
files.

Regards,
Parag.
 
S

Sherman Pendley

Ian Collins said:
Hardly, unless your build system is grossly underspecified.

Unnecessary includes won't have a significant effect on compile times - but
they can certainly hurt build times.

Suppose a method in class Foo accepts arguments of class Bar, but Foo isn't
a subclass of Bar. If we "#include "Bar.h"" in Foo.h, that creates a chain
of dependencies, where any file that includes Foo.h also becomes dependent
on Bar.h. Classes that include Foo.h, but never call the method that takes
Bar arguments, or work with instances of Bar in any way, will still have to
be recompiled when Bar.h is modified.

On the other hand, if we forward declare "class Bar" in Foo.h, and move the
include into Foo.cc, that eliminates the unnecessary dependencies. A change
in Bar.h will still trigger a compile of Foo.cc, of course, but not of other
files that happen to include Foo.h, but don't need the full definition of
Bar.

In both cases, the compile time for Foo.cc is more or less identical. But
the time for the incremental build after changing Foo.h will be improved
by eliminating the unnecessary include.

sherm--
 
J

Jack Klein

Hi,

I would like to know if there is any way / tool available by which we
can detect unnecessary header files included in the source files.

Regards,
Parag.

PC Lint from Gimpel (www.gimpel.com) can do this. It is a commercial
product, not free or open source. I have never actually used it for
this purpose on C++, but I know it works quite well in C.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
Z

Zara

PC Lint from Gimpel (www.gimpel.com) can do this. It is a commercial
product, not free or open source. I have never actually used it for
this purpose on C++, but I know it works quite well in C.

It does also work on C++,, quite nicely.

Best regards,

Zara
 
Y

Yannick Tremblay

Why bother?


Do you have the same opinion on:

- unused variables
- commented out unused code
- not commented out but unreachable code
- duplicated code
- unchecked error return code
- input validation
etc

Yan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top