By Anycorn

2010-02-08 19:40:28 8 Comments

Is there a way for gcc/g++ to dump its preprocessor defines from the command line? I mean things like __GNUC__, __STDC__, and so on.


@philant 2010-02-08 19:42:58

Yes, use -E -dM options instead of -c. Example (outputs them to stdout):

 gcc -dM -E - < /dev/null

For C++

 g++ -dM -E -x c++ - < /dev/null

From the gcc manual:

Instead of the normal output, generate a list of `#define' directives for all the macros defined during the execution of the preprocessor, including predefined macros. This gives you a way of finding out what is predefined in your version of the preprocessor. Assuming you have no file foo.h, the command

touch foo.h; cpp -dM foo.h

will show all the predefined macros.

If you use -dM without the -E option, -dM is interpreted as a synonym for -fdump-rtl-mach.

@Pavel P 2014-01-12 01:29:33

gcc exists on systems where /dev/null means nothing.

@philant 2014-01-12 09:16:17

@Pavel then you can use an empty file, either with gcc or the preprocessor - cpp.

@Pavel P 2014-01-13 20:30:16

I added a more portable approach as an alternative answer: echo | gcc -dM -E - works on windows as well.

@edam 2014-04-17 14:18:20

Is it possible to determine where (i.e., in which file) those defines came from?

@Mark Lakata 2017-01-27 00:40:26

@edam To determine source of macro, look here:…

@René Nyffenegger 2017-03-08 07:41:16

Alternatively, on Windows, cpp -dM -E - < NUL can be used.

@Carlo del Mundo 2017-11-28 00:21:17

Don't forget the '-x c++' command line flag if you're printing c++ define macros. The complete command is: 'g++ -x c++ -dM -E - < /dev/null' for C++ specific macros.

@Mark Adler 2018-12-17 18:40:41

One of the most useful answers, ever. I keep coming back here to get the incantation.

@Pavel P 2014-01-12 01:32:33

A portable approach that works equally well on Linux or Windows (where there is no /dev/null):

echo | gcc -dM -E -

For c++ you may use (replace c++11 with whatever version you use):

echo | gcc -x c++ -std=c++11 -dM -E -

It works by telling gcc to preprocess stdin (which is produced by echo) and print all preprocessor defines (search for -dletters). If you want to know what defines are added when you include a header file you can use -dD option which is similar to -dM but does not include predefined macros:

echo "#include <stdlib.h>" | gcc -x c++ -std=c++11 -dD -E -

Note, however, that empty input still produces lots of defines with -dD option.

@Pavel P 2014-01-29 18:42:12

@rubenvb it's irrelevant. the point is to have cmd line that works equally well on windows and unix at least. If you use NUL, you are back to square one: it won't work on systems that do not have it.

@Xeverous 2017-07-30 11:56:49

adding full answer for C++, works on both Windows and Linux (although sort behaves little different): echo | gcc -x c++ -std=c++17 -dM -E - | sort

@Lennart Rolland 2018-07-25 19:45:10

This produces empty output in git-bash.

@Pavel P 2018-07-26 00:59:01

@LennartRolland does it have gcc? In my git-bash I cannot run gcc

@Lennart Rolland 2018-07-26 07:23:44

@pavel I use mingw32 gcc that comes with Qt5 binary distribution for windows.

@Pavel P 2018-07-26 21:53:58

Don't know what to say. Mingw64 that comes with msys2 works for me: echo | x86_64-w64-mingw32-gcc.exe -dM -E -. Try to run it in cmd.exe to verify that it all works properly?

@Piotr Pałucki 2017-08-25 10:48:11

While working in a big project which has complex build system and where it is hard to get (or modify) the gcc/g++ command directly there is another way to see the result of macro expansion. Simply redefine the macro, and you will get output similiar to following:

file.h: note: this is the location of the previous definition
#define MACRO current_value

@Welgriv 2019-02-13 09:51:49

I am looking for what warning flag is used for it. Do you know ?

@hermannk 2015-01-16 09:28:01

The simple approach (gcc -dM -E - < /dev/null) works fine for gcc but fails for g++. Recently I required a test for a C++11/C++14 feature. Recommendations for their corresponding macro names are published at But:

g++ -dM -E - < /dev/null | fgrep __cpp_alias_templates

always fails, because it silently invokes the C-drivers (as if invoked by gcc). You can see this by comparing its output against that of gcc or by adding a g++-specific command line option like (-std=c++11) which emits the error message cc1: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C.

Because (the non C++) gcc will never support "Templates Aliases" (see you must add the -x c++ option to force the invocation of the C++ compiler (Credits for using the -x c++ options instead of an empty dummy file go to yuyichao, see below):

g++ -dM -E -x c++ /dev/null | fgrep __cpp_alias_templates

There will be no output because g++ (revision 4.9.1, defaults to -std=gnu++98) does not enable C++11-features by default. To do so, use

g++ -dM -E -x c++ -std=c++11 /dev/null | fgrep __cpp_alias_templates

which finally yields

#define __cpp_alias_templates 200704

noting that g++ 4.9.1 does support "Templates Aliases" when invoked with -std=c++11.

@yuyichao 2015-02-16 03:56:03

You don't have to use a dummy file. GCC supports the -x argument so g++ -x c++ -dM -E -std=c++11 - < /dev/null | grep cpp should work.

@hermannk 2015-02-17 07:43:30

@yuyichao Thank you, this makes it easier to use. I wasn't aware of the -x option. Upvoted your comment and integrated it into the original answer.

@Digital Trauma 2013-11-05 00:31:57

Late answer - I found the other answers useful - and wanted to add a bit extra.

How do I dump preprocessor macros coming from a particular header file?

echo "#include <sys/socket.h>" | gcc -E -dM -

In particular, I wanted to see what SOMAXCONN was defined to on my system. I know I could just open up the standard header file, but sometimes I have to search around a bit to find the header file locations. Instead I can just use this one-liner:

$ echo "#include <sys/socket.h>" | gcc -E -dM - | grep SOMAXCONN
#define SOMAXCONN 128

@Paul R 2010-02-08 19:56:44

I usually do it this way:

$ gcc -dM -E - < /dev/null

Note that some preprocessor defines are dependent on command line options - you can test these by adding the relevant options to the above command line. For example, to see which SSE3/SSE4 options are enabled by default:

$ gcc -dM -E - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSSE3__ 1

and then compare this when -msse4 is specified:

$ gcc -dM -E -msse4 - < /dev/null | grep SSE[34]
#define __SSE3__ 1
#define __SSE4_1__ 1
#define __SSE4_2__ 1
#define __SSSE3__ 1

Similarly you can see which options differ between two different sets of command line options, e.g. compare preprocessor defines for optimisation levels -O0 (none) and -O3 (full):

$ gcc -dM -E -O0 - < /dev/null > /tmp/O0.txt
$ gcc -dM -E -O3 - < /dev/null > /tmp/O3.txt
$ sdiff -s /tmp/O0.txt /tmp/O3.txt 
#define __NO_INLINE__ 1        <
                               > #define __OPTIMIZE__ 1

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] What is the difference between g++ and gcc?

  • 2008-10-05 20:25:13
  • Brian R. Bondy
  • 434925 View
  • 831 Score
  • 10 Answer
  • Tags:   c++ gcc g++

12 Answered Questions

23 Answered Questions

[SOLVED] How to get rid of `deprecated conversion from string constant to ‘char*’` warnings in GCC?

  • 2008-09-12 18:15:55
  • Josh Matthews
  • 414586 View
  • 402 Score
  • 23 Answer
  • Tags:   c++ string gcc

9 Answered Questions

[SOLVED] Using GCC to produce readable assembly?

  • 2009-08-17 19:22:17
  • James
  • 156541 View
  • 242 Score
  • 9 Answer
  • Tags:   c gcc assembly

4 Answered Questions

[SOLVED] How to add a default include path for GCC in Linux?

1 Answered Questions

[SOLVED] Visual C++ dump preprocessor defines

5 Answered Questions

[SOLVED] Why does the C preprocessor interpret the word "linux" as the constant "1"?

2 Answered Questions

[SOLVED] gcc: passing list of preprocessor defines

6 Answered Questions

[SOLVED] C preprocessor Macro defining Macro

Sponsored Content