How to process C++ file to remove ifdef'd out code
<p>I have inherited a piece of C++ code which has many <code>#ifdef</code> branches to adjust the behaviour depending on the platform (<code>#ifdef __WIN32</code>, <code>#ifdef __APPLE__</code>, etc.). The code is unreadable in its current form because these preprocessor directives are nested, occur in the middle of functions and even in the middle of multi-line statements.</p>
<p>I'm looking for a way of somehow specifying some preprocessor tags and getting out a copy of the code as if the code had been pre-processed with those flags. I'd like the #include directives to be left untouched, though.
Example:</p>
<pre><code>#include &lt;iostream&gt;

#ifdef __APPLE__
std::cout &lt;&lt; &quot;This is Apple!&quot; &lt;&lt; std::endl;
#elif __WIN32
std::cout &lt;&lt; &quot;This is Windows&quot; &lt;&lt; std::endl;
#endif
</code></pre>
<p>would turn into:</p>
<pre><code>#include &lt;iostream&gt;

std::cout &lt;&lt; &quot;This is Apple!&quot; &lt;&lt; std::endl;
</code></pre>
<p>after being processed by: <code>tool_i_want example.cpp __APPLE__</code>.</p>
<p>I've hacked a quick script that does something similar, but I'd like to know of better tested and more thorough tools. I am running a Linux distribution.</p>
<p>I have decided against just running the C-preprocessor because if I'm not mistaken it will expand the header files, which would make everything more unreadable.</p>