title: Get all include files of a cpp file considering preprocessor defines (fast)
body: <p>I need a tool (command line, script or source code) that extracts all inlcude files that are included by a source file (recursive) with given preprocessor defines and include paths. I want to know the ones that could be found and the one that doesn't. The include files that could be found shall be recursivly parsed.</p>
  
  <p>I know about the </p>
  
  <pre><code>    gcc -M /-MM       
      cl /P
  </code></pre>
  
  <p>solution, but this does not work for me. The preprocessor stops as soon as it could not open a file. But at this time I don't have the correct path for that files and just want the preprocessor to skip that file and to tell me that it could not include that file</p>
  
  <p>Also the <strong>cinclude2dot.pl</strong> from <a href="http://flourish.org/cinclude2dot/" rel="nofollow">here</a> is not useful, because it seems not to consider given preprocessor defines.</p>
  
  <p>Very useful is the <strong>include file hierarchy finder</strong> from <a href="http://www.codeproject.com/Articles/3478/Include-File-Hierarchy-Viewer" rel="nofollow">CodeProject</a>. It considers the preprocessor flags and shows me all include files. Even the includes that couldn't be opened. But it is written in MFC and I would have to reimplement this for the gcc what is not such simple because a lot of WinAPI stuff is used even inside the parser.</p>
  
  <p>Thus, maybe some one knows another solution.</p>
  
  <p>an simple example:</p>
  
  <p>main.cpp</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/foreach.hpp&gt;
  
  #include &lt;SharedClass.h&gt;
  #include "MyClass.h"
  
  #ifdef FOO
      #include &lt;OptClass.h&gt;
  #endif
  
  int main() {}
  </code></pre>
  
  <p>Right now I start the include extraction like (simplified):</p>
  
  <pre><code>.getAllIncludes main.cpp -I includepath1;includepath2 -PD FOO
  </code></pre>
  
  <p>and obtain:</p>
  
  <ul>
  <li>cannot open  //-> I don't care right now, it's a default header</li>
  <li>cannot open   // -> here I can extract the info that I need boost</li>
  <li>SharedClass.h</li>
  <li>SharedDependenyClass.h //a header that is included by SharedClass...</li>
  <li>MyClass.h</li>
  <li>TestClass.h //a header that is included by the MyClass header...</li>
  <li>TestClass2.h //a header that is included by the TestClass header...</li>
  <li>OptClass.h</li>
  </ul>
  
  <p>and for</p>
  
  <pre><code>.getAllIncludes main.cpp -I includepath1;includepath2 
  </code></pre>
  
  <p>I'll obtain:</p>
  
  <ul>
  <li>cannot open  //-> I don't care right now, it's a default header</li>
  <li>cannot open   // -> here I can extract the info that I need boost</li>
  <li>SharedClass.h</li>
  <li>SharedDependenyClass.h //a header that is included by SharedClass...</li>
  <li>MyClass.h</li>
  <li>TestClass.h //a header that is included by the MyClass header...</li>
  <li>TestClass2.h //a header that is included by the TestClass header...</li>
  </ul>
  
  <p>I know that the deafault header may also define some values. But in my case I don't need that information, because the project source code doesn't depend on any of that defines. If thus, I feed my tool with this preprocessor define...</p>
  
  <p>In the end the tool works quite well. It runs recursivly over ALL necessary files and in the end I have all needed files for the project. Of course there are some small restrictions I don't want to name then all (e.g. every header of an source file name has the same name, ... ).</p>