开发者

C/C++: Removing unnecessary includes [duplicate]

开发者 https://www.devze.com 2023-01-05 18:38 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicates: C/C++: Detecting superfluous #includes?
This question already has answers here: Closed 12 years ago.

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 tec开发者_如何学Gohnique 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消