0

Possible Duplicates:
C/C++: Detecting superfluous #includes?
How should I detect unnecessary #include files in a large C++ project?

I'm looking to do some house cleaning in our code base. I would like to start by removing all unnecessary header includes from our source files (*.c and *.cpp). Does anyone know of a tool or technique for doing this?

We are using GCC on Mac, Linux and Solaris. Using Visual Studio on Windows. I looked through the documentation of both compilers and there did not seem to be a option to make it warn for unnecessary includes.

Any thoughts or advice are appreciated.

Community
  • 1
  • 1

1 Answers1

0

I had to do this once and I ended up doing this:

  1. I removed almost every #include statements from both my haeder and source files
  2. I tried to compile my source code
  3. Whenever the compiler complains about something undefined, I either:

    • Added a minimal declaration instead of a header inclusion wherever I could (something like: "class SomeClass;" instead of `#include "someclass.hpp")
    • Added the required include when I had no other choice.
  4. Go to 2. until it succeeds.

I admit this is long and boring, but it worthed it.

ereOn
  • 48,328
  • 33
  • 147
  • 228
  • The problem with that is there are over 5000 source files and a total of 22 projects. Because there is a lot of common code between the various projects I would have to rebuild each project each time I need to test changes. At that point it's not even about the man hours, it's about waiting 1.5 hours to compile them all and try again. I fear this is what's going to end up happening, but I was hoping for an easier, automated solution. – Wesley Workman Jul 01 '10 at 16:14