Which C++ standard is the default when compiling with g++?

后端 未结 9 544
故里飘歌
故里飘歌 2020-11-29 23:59

I have a piece of code that looks like the following. Let\'s say it\'s in a file named example.cpp

#include 
#include 

        
相关标签:
9条回答
  • 2020-11-30 00:21

    I'm guessing a default version of the C++ compiler gets called, but I don't know which?

    This is only guessable by reading the documentation of your particular compiler version.

    If using a recent GCC, I recommend first to understand what version are you using by running

    g++ -v
    

    or

    g++ --version
    

    and then refer to the version of the particular release of GCC. For example for GCC 7, read GCC 7 changes etc

    Alternatively, run

    g++ -dumpspecs
    

    and decipher the default so called spec file.

    BTW, you could ensure (e.g. in some of your common header file) that C++ is at least C++17 by coding

     #if __cplusplus < 201412L
     #error expecting C++17 standard
     #endif
    

    and I actually recommend doing it that way.

    PS. Actually, think of C++98 & C++17 being two different languages (e.g. like Ocaml4 and C++11 are). Require your user to have a compiler supporting some defined language standard (e.g. C++11), not some particular version of GCC. Read also about package managers.

    0 讨论(0)
  • 2020-11-30 00:23

    If your version of g++ is later than 4.7 I think you can find the default version of C++ standard supported like so:

    g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
    

    An example from my machine:

    mburr@mint17 ~ $ g++ --version | head -1
    g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
    mburr@mint17 ~ $ g++ -dM -E -x c++  /dev/null | grep -F __cplusplus
    #define __cplusplus 199711L
    

    Some references:

    • Details on the g++ options used
    • Why this only works for g++ 4.7 or later
    0 讨论(0)
  • 2020-11-30 00:29

    g++ man page actually tells what is the default standard for C++ code.

    Use following script to show the relevant part:

    man g++ | col -b | grep -B 1 -e '-std.* default'
    

    For example, in RHEL 6 g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-23), the output:

             gnu++98
               GNU dialect of -std=c++98.  This is the default for C++ code.
    

    And in Fedora 28 g++ (GCC) 8.1.1 20180502 (Red Hat 8.1.1-1), the output:

           gnu++1y
               GNU dialect of -std=c++14.  This is the default for C++ code.  The name gnu++1y is deprecated.
    
    0 讨论(0)
提交回复
热议问题