How to See the Output of the Visual C++ Preprocessor

How can I see the output of the Visual C++ preprocessor?

cl.exe, the command line interface to Microsoft Visual C++, has three different options for outputting the preprocessed file (hence the inconsistency in the previous responses about Visual C++):

  • /E: preprocess to stdout (similar to GCC's -E option)
  • /P: preprocess to file
  • /EP: preprocess to stdout without #line directives

(copied directly from https://stackoverflow.com/a/277362/3279)

How do I see a C/C++ source file after preprocessing in Visual Studio?

cl.exe, the command line interface to Microsoft Visual C++, has three different options for outputting the preprocessed file (hence the inconsistency in the previous responses about Visual C++):

  • /E: preprocess to stdout (similar to GCC's -E option)
  • /P: preprocess to file
  • /EP: preprocess to stdout without #line directives

If you want to preprocess to a file without #line directives, combine the /P and /EP options.

See what the preprocessor is doing

For gcc just use the -E switch

gcc -E

-E Stop after the preprocessing stage; do not run the compiler proper.
The output is in the form of
preprocessed source code, which is
sent to the standard output.

Preprocessor output

gcc -E file.c

or

g++ -E file.cpp

will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output.

Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output.

For Visual C++ the switch is /E which spits the preprocessor output to screen.

How to print macros in msvc preprocessor?

There is an undocumented compiler switch, /d1PP, that will retain macro definitions in the preprocessor output. If you compile Rado's example with /P /d1PP, you'll get the following output file:

#line 1 "q.cpp"
#define BLAH 1

int main()
{

cout << "BLAH defined" << endl;

#line 10 "q.cpp"
}

Note that this switch is not documented. It is thus not officially supported. It may be removed at any time, or its behavior may be changed at any time. It may have bugs. Use at your own risk, etc., etc.

Is it possible to print a preprocessor variable in C?

You can print out the value of a preprocessor variable under visual studio. The following prints out the value of _MSC_VER:

#define STRING2(x) #x
#define STRING(x) STRING2(x)

#pragma message(STRING(_MSC_VER))

Not sure how standard this is though.

How to figure out what value MSVC is using for a preprocessor macro

Try one of the following options to CL.exe:

/E preprocess to stdout
/P preprocess to file

If you're building within Visual Studio, you can specify custom command-line options in one of the project property dialogs.

How to follow preprocessor directives throughout a compilation

I think you're on the right track with the /P option, but the doc states that the output will go to files with a .i extension (so probably not to your Output window).

Further, you may be able to find some help on any linker errors by generating a map file; see /MAP (Generate Mapfile).



Related Topics



Leave a reply



Submit