How do I see a C/C++ source file after preprocessing in Visual Studio?
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++):
-
/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.
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.
Related videos on Youtube
MarkD
Updated on February 17, 2020Comments
-
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 over 15 yearsIt would help if you provided more information such as which compiler you are using.
-
MarkD over 15 yearsI'm currently using Visual Studio 2005 as my IDE .
-
Martin York over 15 yearsThen by default you are using the cl.exe compiler.
-
Jim Buck over 15 yearsWhy was a gcc answer accepted when he said he was using VS2005?
-
Laurie Stearn over 4 yearsRelated: Compiler info.
-
-
Rob W over 10 years"Preprocess to a File" in VS2010.
-
Steed over 10 yearsBTW, the file is created in output directory (e.g.
Release
,Debug
) with an extension.i
. -
WooDzu about 10 yearsHow do I pass these commands to a project with nmake and Makefile?
-
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 over 8 yearsThis also works with gcc-derived compilers, such as qcc.exe - thanks!
-
IanH over 8 yearsThis 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 over 8 yearsgcc question: stackoverflow.com/questions/3916979/…
-
smwikipedia over 7 yearsPlease 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 over 6 yearsNote, 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 over 4 yearsMake sure you are in
VS Developer command prompt
and NOT inCommand prompt
when trying to use MS compiler (cl
). Compiler is not available in conventional comand prompt. -
Laurie Stearn over 4 yearsVS2019 Community: The /P LNK 1104 error is still hot off the press.