GCC dump preprocessor defines

后端 未结 6 2211
既然无缘
既然无缘 2020-11-22 14:28

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.

6条回答
  •  我在风中等你
    2020-11-22 15:10

    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.

提交回复
热议问题