This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.


  Preprocessed Source Code (MSVC)
  Submitted by

While browsing deep into the bowels of the MSDN library, I found a blurb about outputing Preprocessed source code. I somehow managed to miss these switches in the compiler reference..oops! Apparently, the compiler switches /E, /P, and /EP allow you to view source code that has been preprocessed.

For the uninformed, viewing your preprocessed code can help make sure that all macros are expanded properly and that the correct directives are used (#ifdef...#endif). Of couse, browsing through thousands and thousands of a game engine's processed code just to inspect macro expansions and directives might be a little overkill...but if you know that a certain piece of code is failing, checking macro expansions can help catch some sneaky bugs.

Here's a simple example of a poorly written macro:
#define BAD_CUBE(x) x*x*x
// Correct version: #define GOOD_CUBE(x) ((x)*(x)*(x))
int i = 5;
int i_cubed = BAD_CUBE(i+2));
// BAD_CUBE expands to: i+2*i+2*i+2 
// In BAD_CUBE, (*) has higher precedence than (+), so the code
// ends up like this: i + (2*i) + (2*i) +2
// GOOD_CUBE expands to: ((i+2)*(i+2)*(i+2))

Here are the combinations:

Option Output with #lines? Output

/E Yes stdout /P Yes *.i file /EP No stdout /E /EP No stdout /P /EP No *.i file

To preserve comments during preprocessing, add the /C switch. Example: CL /P /C myfile.cpp // outputs to "myfile.i"

I don't know if any other compilers support a similar compiler switch. But if you know of one that does, let me know! :)

I'm sure all of you programming gurus already knew about this switch, but newbies like myself can use all the help we can get.<grin>

-|Ben Odam|-

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.


Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.