1 out of 4 rated this helpful - Rate this topic

/P (Preprocess to a File)

Preprocesses C and C++ source files and writes the preprocessed output to a file.

/P

The file has the same base name as the source file and an .i extension. In the process, all preprocessor directives are carried out, macro expansions are performed, and comments are removed. To preserve comments in the preprocessed output, use the /C (Preserve Comments During Preprocessing) option along with /P.

/P adds #line directives to the output, at the beginning and end of each included file and around lines removed by preprocessor directives for conditional compilation. These directives renumber the lines of the preprocessed file. As a result, errors generated during later stages of processing refer to the line numbers of the original source file rather than lines in the preprocessed file. To suppress the generation of #line directives, use /EP (Preprocess to stdout Without #line Directives) as well as /P.

The /P option suppresses compilation. It does not produce an .obj file, even if you use /Fo (Object File Name). You must resubmit the preprocessed file for compilation. /P also suppresses the output files from the /FA, /Fa, and /Fm options. For more information, see /FA, /Fa (Listing File) and /Fm (Name Mapfile).

To set this compiler option in the Visual Studio development environment

  1. Open the project's Property Pages dialog box. For details, see How to: Open Project Property Pages.

  2. Click the C/C++ folder.

  3. Click the Preprocessor property page.

  4. Modify the Generate Preprocessed File property.

To set this compiler option programmatically

The following command line preprocesses ADD.C, preserves comments, adds #line directives, and writes the result to a file, ADD.I:

CL /P /C ADD.C
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
The file generated by /P is not 100% correct
The file generated by /P is not 100% correct.  The problems are:
1. /* */ comment, even if it spans multiple lines, should be replaced by exactly ONE space according to msdn documentation and the standard.  But the file created by /P is still keeping the newlines if the /* */ spans multiple lines.  This may or may not cause error if we resubmit that file for compilation. e.g.
abc /**
comments
*/ edf
should be equivalent to "abc   edf" - 1 single line with all the newlines in the comment replaced by ONE space.  But the one generated by /P consists of 3 lines
abc

 edf


2.  The line continuation (\  at the end of a line) should cause the lines concatenated before preprocessing.  But currently the output fiile doesn't eg
abc \
def
is still generated as
abc \
def
Should it become "abc def"?


MSDN c/c++ preprocessor reference->preprocessor ->phases of translation
Tokenization
The source file is broken into preprocessing tokens and white-space characters. Comments in the source file are replaced with one space character each. Newline characters are retained

The Visual Studio development team should pay attention that the last sentence above should be read independently.  It got nothing to do with comment processing.  I bet someone read it incorrectly while developing the preprocessor.
Advertisement