views:

112

answers:

3

In Visual C++, one can find the header file where any name (variable or type) is defined by pressing F12 on it or choosing Go to Definition. This feature is very useful, but it only shows the final location (header file) where the name is defined. Is there a way to figure out the chain of header files that lead from my source file to the final header file for a given name?

For example, consider this code:

// main.cpp    
#include <stddef.h>
int main()
{
    size_t s;
    return 0;
}

In Visual C++ 2010, if I look up the definition of size_t in the above main.cpp, it lands me in some file named sourceannotations.h. I know that this header chain begins with stddef.h (which I have included) and ends in sourceannotations.h. How to figure out the links in the middle of this chain?

+1  A: 

You can use reverse engineering tools like Doxygen, Understand Analyst etc. This will help you understand the full flow of variables, function calls.

ckv
+1 for referring to Understand for C/C++, but this still requires you to open the include chart in Understand and click-open every include until you found the include file where the variable/type/constant is actually defined. So, still lots of manual work. But, on the other hand, indeed very good utility.
Patrick
+1  A: 

In your properties dialog, under C/C++, Preprocessor, enable Preprocess to a File. If you compile main.cpp, this will generate main.i.

You can then look in main.i and see which file includes what other file.

R Samuel Klatchko
That was also my initial idea (using the /P compiler option), but from that generated .i file I think it is still difficult to see what the actual 'chain' is.
Patrick
+5  A: 
  1. Right click project, "Project Properties"
  2. "Configuration Properties" -> "C/C++" -> "Advanced".
  3. Set "Show Includes" to "Yes".

The complete hierarchy of headers will be printed out in the output window when you compile every file.

Alex B
This works! It does not show the chain for a certain name, but almost suits my need. Thanks! :-)
Ashwin
For the curious, the header chain for size_t in VC++ 2010 turned out to be: stddef.h -> crtdefs.h -> sal.h -> sourceannotations.h
Ashwin
@Ashwin: you won't get an exact chain of dependencies, because the header may be included from multiple places. What's worse, C++ allows duplicate typedefs to the same type. Now you have a tree, instead of a chain.
Alex B
Alex: Yes, I realize that for any real-world example the inclusion might have occurred from anywhere. But, this is still a useful tool to investigate the header chain.
Ashwin