views:

219

answers:

3

In Visual Studio (C++), is there a way to easily find duplicate headers that are defined in .cpp files?

I'm also trying to find ways to detect this situation:

  • A includes B includes C
  • A includes C
  • => A doesn't need to include C
+5  A: 

If you want to detect this situation you could add this macro to the top of every file. Substitute A for the name of the file

#if A_H
#error "Duplicate include"
#else
#define A_H
#endif
JaredPar
That's a clever solution. I haven't seen that technique before. I would suggest using #warning though, since eliminating ALL duplicate includes can result in scenarios that a far uglier than the problem this solves.
Shmoopty
+4  A: 

Generally, you mark them with #pragma once, or the equivalent macro guard and stop caring about it.

If you're using MS compiler, you can put a #pragma message (IIRC, it might be a #pragma warning or #pragma error instead) with the name of the header file at the very top of each header file and your build output will show the list of every one that's being included per file that's compiled.

That would work best for your header files, as the system ones will not be included.

If you want to see all included headers, edit the compile options to include /P (preprocess to a file) that will not compile your code, but instead will redirect the pre-processer to a file, you can then grep through it to see the #include statements. Be aware these files will be large.

gbjbaanb
A: 

PC-Lint (and probably other lint tools) can indicate unused include files.

Steve Fallows