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.
相关问题
- Implicitly defined constructor deleted due to vari
- Error building gcc 4.8.3 from source: libstdc++.so
- What are the recommended GNU linker options to spe
- What is the right order of linker flags in gcc?
- Why doesn't g++ -Wconversion warn about conver
相关文章
- gcc/g++ gives me error “CreateProcess: No such fil
- Calls that precede a function's definition can
- How can I use gcc's -I command to add recursiv
- How do I know if std::map insert succeeded or fail
- How to specify gcc flags (CXXFLAGS) particularly f
- How to generate assembly code with gcc that can be
- Embedding a program's source code into its bin
- Using C Preprocessing to get integer value of a st
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: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 messagecc1: 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):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
which finally yields
noting that g++ 4.9.1 does support "Templates Aliases" when invoked with
-std=c++11
.Yes, use
-E -dM
options instead of -c. Example (outputs them to stdout):For C++
From the gcc manual:
A portable approach that works equally well on Linux or Windows (where there is no /dev/null):
For c++ you may use (replace
c++11
with whatever version you use):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:Note, however, that empty input still produces lots of defines with
-dD
option.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?
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:
I usually do it this way:
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:
and then compare this when
-msse4
is specified: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):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: