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

100,206

Solution 1

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

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

Solution 2

Most compilers have an option to just run the preprocessor. e.g., gcc provides -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.

So you can just run:

gcc -E foo.c

If you can't find such an option, you can also just find the C preprocessor on your machine. It's usually called cpp and is probably already in your path. Invoke it like this:

cpp foo.c

If there are headers you need to include from other directories , you can pass -I/path/to/include/dir to either of these, just as you would with a regular compile.

For Windows, I'll leave it to other posters to provide answers as I'm no expert there.

Solution 3

Right-click on the file on the Solution Explorer, goto Properties. Under Configuration Properties->C/C++->Preprocessor, "Generate Preprocessed File" is what you are looking for. Then right-click on the file in the Solution Explorer and select "Compile". The preprocessed file is created in the output directory (e.g. Release, Debug) with an extension .i (thanks to Steed for his comment).

Solution 4

You typically need to do some postprocessing on the output of the preprocessor, otherwise all the macros just expand to one liners, which is hard to read and debug. For C code, something like the following would suffice:

gcc -E code.c | sed '/^\#/d' | indent -st -i2 > code-x.c

For C++ code, it's actually a lot harder. For GCC/g++, I found this Perl script useful.

Solution 5

Try cl /EP if you are using Microsoft's C++ compiler.

Share:
100,206

Related videos on Youtube

MarkD
Author by

MarkD

Updated on February 17, 2020

Comments

  • MarkD
    MarkD about 4 years

    Let's say I have a source file with many preprocessor directives. Is it possible to see how it looks after the preprocessor is done with it?

    • JaredPar
      JaredPar over 15 years
      It would help if you provided more information such as which compiler you are using.
    • MarkD
      MarkD over 15 years
      I'm currently using Visual Studio 2005 as my IDE .
    • Martin York
      Martin York over 15 years
      Then by default you are using the cl.exe compiler.
    • Jim Buck
      Jim Buck over 15 years
      Why was a gcc answer accepted when he said he was using VS2005?
    • Laurie Stearn
      Laurie Stearn over 4 years
      Related: Compiler info.
  • Rob W
    Rob W over 10 years
    "Preprocess to a File" in VS2010.
  • Steed
    Steed over 10 years
    BTW, the file is created in output directory (e.g. Release, Debug) with an extension .i.
  • WooDzu
    WooDzu about 10 years
    How do I pass these commands to a project with nmake and Makefile?
  • G.Rassovsky
    G.Rassovsky about 9 years
    @WooDzu Use NMAKE (nmake.exe) to automate tasks that build Visual C++ projects by using a traditional makefile. ~ msdn.microsoft.com/en-us/library/f35ctcxw.aspx
  • sage
    sage over 8 years
    This also works with gcc-derived compilers, such as qcc.exe - thanks!
  • IanH
    IanH over 8 years
    This helps a lot since it uses the build configuration already set up in the project to find all the right headers.
  • Ciro Santilli OurBigBook.com
    Ciro Santilli OurBigBook.com over 8 years
  • smwikipedia
    smwikipedia over 7 years
    Please note that /P will suppress the generation of obj files. So if you put /P options, you may get link a error saying some obj files cannot be found because it is actually not generated at all.
  • user1556435
    user1556435 over 6 years
    Note, for me on MSVC 2015, the .i file was not in the Output Directory, but in the project's Intermediate Directory. These are not the same if you e.g. generate your projects using CMake.
  • dmytro.poliarush
    dmytro.poliarush over 4 years
    Make sure you are in VS Developer command prompt and NOT in Command prompt when trying to use MS compiler (cl). Compiler is not available in conventional comand prompt.
  • Laurie Stearn
    Laurie Stearn over 4 years
    VS2019 Community: The /P LNK 1104 error is still hot off the press.