By Geo


2008-11-10 07:11:08 8 Comments

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?

10 comments

@Jim Buck 2008-11-10 07:18:50

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).

@Rob W 2013-11-01 09:31:41

"Preprocess to a File" in VS2010.

@Steed 2014-01-10 11:05:42

BTW, the file is created in output directory (e.g. Release, Debug) with an extension .i.

@Assaf Levy 2014-03-12 13:15:37

+1 most straightforward answer.

@IanH 2015-11-06 19:45:15

This helps a lot since it uses the build configuration already set up in the project to find all the right headers.

@user1556435 2017-12-18 16:39:12

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.

@bk1e 2008-11-10 08:33:17

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.

@WooDzu 2014-03-09 08:00:53

How do I pass these commands to a project with nmake and Makefile?

@G.Rassovsky 2015-03-17 09:11:28

@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

@smwikipedia 2016-08-09 02:16:39

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.

@ididak 2008-11-10 08:06:13

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.

@Atempcode 2008-11-10 07:16:14

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

@manty 2015-01-31 02:24:45

On Windows OS, a simple one line answer to this question is to use the below command in DOS prompt to see the preprocessed file:

CL /P /C myprogram.c

This will generate a file called myprogram.i. Open it and look out for your expanded preprocessors.

@thegreendroid 2014-10-04 07:00:42

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.

@dzav 2013-03-06 23:16:57

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.

@sage 2015-11-02 22:48:18

This also works with gcc-derived compilers, such as qcc.exe - thanks!

@Sam Pittman 2013-01-24 19:38:21

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.

@Todd Gamblin 2008-11-10 07:15:27

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.

@Ciro Santilli 新疆改造中心 六四事件 法轮功 2016-01-17 11:44:45

@Andreas Magnusson 2008-11-10 07:15:54

In Visual Studio you can compile a file (or project) with /P.

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Image Processing: Algorithm Improvement for 'Coca-Cola Can' Recognition

10 Answered Questions

[SOLVED] Improve INSERT-per-second performance of SQLite?

12 Answered Questions

[SOLVED] How can I profile C++ code running on Linux?

  • 2008-12-17 20:29:24
  • Gabriel Isenberg
  • 453527 View
  • 1617 Score
  • 12 Answer
  • Tags:   c++ unix profiling

11 Answered Questions

[SOLVED] How do I list the symbols in a .so file

16 Answered Questions

[SOLVED] How do I use extern to share variables between source files?

19 Answered Questions

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 276971 View
  • 1435 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

5 Answered Questions

[SOLVED] What is the copy-and-swap idiom?

14 Answered Questions

[SOLVED] How many levels of pointers can we have?

  • 2012-04-10 10:34:17
  • Parag
  • 42267 View
  • 424 Score
  • 14 Answer
  • Tags:   c++ c pointers

Sponsored Content