Preprocessor output

GccMacrosC Preprocessor

Gcc Problem Overview


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.

Gcc Solutions


Solution 1 - Gcc

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.

Solution 2 - Gcc

You can also call the C Preprocessor directly.

cpp infile outfile

Check out man cpp for more info.

Solution 3 - Gcc

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.

Solution 4 - Gcc

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.

Solution 5 - Gcc

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 https://stackoverflow.com/questions/7146425/llvm-translation-unit)

Solution 6 - Gcc

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser191776View Question on Stackoverflow
Solution 1 - GccFaisal FerozView Answer on Stackoverflow
Solution 2 - GccrdasView Answer on Stackoverflow
Solution 3 - GccAlexander LukichevView Answer on Stackoverflow
Solution 4 - GccBart van Ingen SchenauView Answer on Stackoverflow
Solution 5 - GccRhubarbView Answer on Stackoverflow
Solution 6 - GccMichaelMoserView Answer on Stackoverflow