ChatGPT解决这个技术问题 Extra ChatGPT

GCC dump preprocessor defines

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.


p
proski

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

 echo | gcc -dM -E -
 echo | clang -dM -E -

For C++

 echo | g++ -dM -E -x c++ -
 echo | clang++ -dM -E -x c++ -

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.


gcc exists on systems where /dev/null means nothing.
@Pavel then you can use an empty file, either with gcc or the preprocessor - cpp.
I added a more portable approach as an alternative answer: echo | gcc -dM -E - works on windows as well.
Is it possible to determine where (i.e., in which file) those defines came from?
Alternatively, on Windows, cpp -dM -E - < NUL can be used.
P
Paul R

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

D
Digital Trauma

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 -

or (thanks to @mymedia for the suggestion):

gcc -E -dM -include sys/socket.h - < /dev/null

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:

$ gcc -E -dM -include sys/socket.h - < /dev/null | grep SOMAXCONN
#define SOMAXCONN 128
$ 

you can use -include compiler option to avoid pipes
h
hermannk

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 https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations. 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 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2258.pdf) 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.


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.
@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.
P
Pavel P

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.


@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.
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
This produces empty output in git-bash.
@LennartRolland does it have gcc? In my git-bash I cannot run gcc
@pavel I use mingw32 gcc that comes with Qt5 binary distribution for windows.
P
Piotr Pałucki

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

I am looking for what warning flag is used for it. Do you know ?
Maybe -Wno-builtin-macro-redefined is the one?