37

I want to see the output of the Visual C++ Preprocessor on my code -- i.e., the equivalent of gcc -E. For the life of me, I cannot find the relevant compiler switch. How do I accomplish this?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Benjamin Pollack
  • 27,594
  • 16
  • 81
  • 105

2 Answers2

46

Project properties -> C/C++ -> Preprocessing -> Preprocess to a file: Yes (/P)

The files will be called .i and will be created in the build directory.

Also see the msdn page.

Antonio
  • 19,451
  • 13
  • 99
  • 197
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • 3
    You can specify the preprocessor output file with the /Fi compiler parameter. You can also specify the output path only - just make sure the path ends with a trailing backslash. – MikeOnline Jan 18 '17 at 19:15
  • 6
    @marsh: The files will be called .i and will be created in the build directory. – Daniel Jan 27 '17 at 16:36
  • Where is "Project properties"? Going to `Project` (top menu) > `Properties`, I can't find any `C/C++` menu item. – Iulian Onofrei Apr 22 '18 at 11:14
  • @IulianOnofrei right click on your project (not the solution) in the Solution Explorer window that's usually on the right of the screen and is what you use to navigate through your project. Once you right click, there should be an option called "Properties". That's the project properties. – Pro Q Jun 02 '18 at 01:17
  • 1
    When I did this, I got `error MSB6001: Invalid command line switch for "CL.exe"` – Pro Q Jun 02 '18 at 01:22
23

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++):

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

Community
  • 1
  • 1
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • 30
    Nice copy-paste without even a link to [the original answer](http://stackoverflow.com/a/277362/688659)... – gx_ Jun 26 '13 at 16:27
  • 4
    I just did this, and this answer isn't complete. After setting /P, for file "foo.c" you need to look for the file "foo.i". (This may seem obvious, but you have to look in the right directories, and that depends on how your project files are configured). – Ira Baxter Aug 24 '14 at 08:38