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?
-
It would help if you provided more information such as which compiler you are using.JaredPar– JaredPar2008-11-10 07:14:15 +00:00Commented Nov 10, 2008 at 7:14
-
I'm currently using Visual Studio 2005 as my IDE .Geo– Geo2008-11-10 07:16:01 +00:00Commented Nov 10, 2008 at 7:16
-
Then by default you are using the cl.exe compiler.Loki Astari– Loki Astari2008-11-10 15:37:40 +00:00Commented Nov 10, 2008 at 15:37
-
3Why was a gcc answer accepted when he said he was using VS2005?Jim Buck– Jim Buck2008-11-10 16:49:50 +00:00Commented Nov 10, 2008 at 16:49
-
Related: Compiler info.Laurie Stearn– Laurie Stearn2020-01-17 09:17:10 +00:00Commented Jan 17, 2020 at 9:17
10 Answers
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.
5 Comments
/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.VS Developer command prompt and NOT in Command prompt when trying to use MS compiler (cl). Compiler is not available in conventional comand prompt.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.
1 Comment
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).
4 Comments
Release, Debug) with an extension .i.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.
Comments
Try cl /EP if you are using Microsoft's C++ compiler.
1 Comment
cl.exe you'll probably want several switches similar to this cl /nologo /Zc:preprocessor /E /PD bla.cpp, where bla.cpp can be created as an "empty" file by saying echo.>bla.cpp on the command prompt.I don't know anything about Microsoft compiler, but on GCC you can use this:
gcc -E -P -o result.c my_file.h
If you want to see comments use this:
gcc -E -C -P -o result.c my_file.h
More options avaliable on this page.
1 Comment
As bk1e and Andreas M. answered, the /P option for the compiler will cause it to preprocess a file. However, in my project using VS2005 and Platform Builder (for an embedded ARM processor), the project did not present an option in the dialog box (as described by Jim B) to enable that option.
I could run CL manually and add /P, but it failed because I did not know all of the appropriate command-line options that were invisibly being activated by Platform Builder during the full build. So I needed to know all of those options.
My solution was to go look in the build.log file, and find the line that executed
CL blah-blah-blah myfile.c
I copied this line to the clipboard. The "blah-blah-blah" part contained the build options, and was huge.
Back in the IDE, I right-clicked on myfile.c, chose "Open Build Window", and then in that window I pasted the build command-line, and added a "/P".
CL /P blah-blah-blah myfile.c
Done. The myfile.i file was produced, which contained the preprocessor output.
Comments
CPIP is a new C/C++ preprocessor written in Python. If you want a detailed visual representation of a preprocessed file, give it a shot.
CPIP is a C/C++ pre-processor implemented in Python. Most pre-processors regard pre-processing as a dirty job that just has to be done as soon as possible. This can make it very hard to track down subtle defects at the pre-processing stage as pre-processors throw away a lot of useful information in favor of getting the result as cheaply as possible.
Few developers really understand pre-processing, to many it is an obscure bit of black magic. CPIP aims to improve that and by recording every detail of preprocessing so CPIP can can produce some wonderfully visual information about file dependencies, macro usage and so on.
CPIP is not designed to be a replacement for cpp (or any other established pre-processor), instead CPIP regards clarity and understanding as more important than speed of processing.