views:

1136

answers:

6

Problem: I have a large Visual C++ project that I'm trying to migrate to Visual Studio 2010. It's a huge mix of stuff from various sources and of various ages. I'm getting problems because something is including both winsock.h and winsock2.h.

Question: What tools and techniques are there for displaying the #include hierarchy for a Visual Studio C++ source file?

I know about cl /P for getting the preprocessor output, but that doesn't clearly show which file includes which other files (and in this case the /P output is 376,932 lines long 8-)

In a perfect world I'd like a hierarchical display of which files include which other files, along with line numbers so I can jump into the sources:

source.cpp(1)
  windows.h(100)
    winsock.h
  some_other_thing.h(1234)
    winsock2.h
+1  A: 

Here is a good 3rd-party, FOSS tool. You can export results to XML, which will include data on number of occurrences and line numbers.

pianoman
It says it supports VC7, does it support 2005/2008? It looks a bit stale...
polyglot
+1  A: 

cl /P should show you the line numbers, such that you can tell the context of where a header file is being included from.

If you grep out the lines with ...

grep "^#line" file.i

... then you should have a pretty clean indication of what files were encountered in order by the preprocessor.

If it's a one off incident this should be a pretty quick diagnostic.

polyglot
Sure, but that gives me eight thousand lines of unstructured output, with no hierarchy.
RichieHindle
I thought you said it was a one-off bug years - I don't get your point.The bug you describe has occurred to me with winsock/winsock2 also, but that type of thing only happens once every 2 years so one hardly needs a slick process to solve the problem.If you had asked the question in the general rather than the specific (how do I solve the winsock.h problem) I would not have submitted the answer.
polyglot
BTW, show includes above is a much better solution :)
polyglot
I'd been looking through the cl /P output for quite long enough, and wondered whether there was a better tool for the job. Now I've discovered that there is, which is great. The question *is* general, and the answers will be here on SO forever, for others to find.
RichieHindle
+10  A: 

There is a C/C++ -> Advanced project setting "show Includes". That will generate the tree. It maps to the compiler switch /showIncludes

xtofl
That's just what I was looking for (barring line numbers, but you can't have everything). Silly me, I was looking under the Preprocessor options. 8-) Thanks!
RichieHindle
Excellent - I had missed that.
polyglot
Note: the hierarchy can be seen in the Output window.
CannibalSmith
+4  A: 

The compiler also supports a /showIncludes switch -- it doesn't give you line numbers, but can give a pretty comprehensive view of which includes come from where.

It's under Project Settings -> Configuration Properties -> C/C++ -> Advanced -> Show Includes.

Kim Gräsman
+1 Many thanks! (But I'm afraid xtofl gets the Accept for being quicker).
RichieHindle
+4  A: 

We have found IncludeManager to be a very powerful tool. It is not free (but not expensive) and it allowed us to get a grip of our Include issues and drop our compile time from 50 minutes to 8 minutes by pruning out large chunks of includes we weren't using.

Colin Desmond
@Colin: That looks very useful indeed - thanks!
RichieHindle
Yow! I ran IncludeManager on the offending file, and it produced a graph that made me laugh out loud. By my calculations I would need a 400" monitor to see the whole thing. I think we're beyond its power to help. 8-)
RichieHindle
A: 

Try redhat Source-Navigator for a more graphical solution.

Vulcan Eager