77

How do I view the output produced by the C pre-processor, prior to its conversion into an object file?

I want to see what the MACRO definitions do to my code.

Brian Tompsett - 汤莱恩
  • 5,438
  • 68
  • 55
  • 126

6 Answers6

103
gcc -E file.c

or

g++ -E file.cpp

will do this for you. The -E switch forces the compiler to stop after the preprocessing phase, spitting all it’s got at the moment to standard output.

Note: Surely you must have some #include directives. The included files get preprocessed, too, so you might get lots of output.

For Visual C++ the switch is /E which spits the preprocessor output to screen.

Faisal Feroz
  • 11,886
  • 4
  • 37
  • 51
17

You can also call the C Preprocessor directly.

cpp infile outfile

Check out man cpp for more info.

rdas
  • 18,048
  • 6
  • 31
  • 42
  • Nice! I prefer this one over `cc -E`. Also, I use `-P` to disable line markets too (works with `cc` as well). – ziya Feb 21 '20 at 18:13
9

For GCC,

gcc -E -dM file.c

or

g++ -E -dM file.cpp

should do the job. -dM, as GNU Preprocessor manual puts it, should generate a list of ‘#define’ directives for all the macros defined during the execution of the preprocessor, including predefined macros.

3

It depends on the compiler you use.
With GCC, you can specify the -E flag on the command-line to let the compiler produce the pre-processor output.

Bart van Ingen Schenau
  • 15,018
  • 4
  • 30
  • 41
1

If using CLion by Jetbrains, you can use the action "clangd: Preprocess current TU"

So hit shift shift and start typing clangd...

action popup

Best assign it to a shortcut for simpler reuse in preferences->keymap:

enter image description here

Shout out to marcosbento

PS: TU means 'translation unit' (see here LLVM translation unit)

Rhubarb
  • 33,324
  • 2
  • 45
  • 33
0

You can check out my script described here:

http://mosermichael.github.io/cstuff/all/projects/2011/09/16/preprocessor.html

It formats the preprocessor output into a (hopefully) readable html document: lines that are different due to preprocessor are marked in the file.

MichaelMoser
  • 2,894
  • 1
  • 22
  • 24