Compiler standards support (c++11, c++14, c++17)

70,920

Solution 1

So, after a bit of a struggle trying to dust off my sed skills, I was able to come up with this command:

gcc -v --help 2> /dev/null | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

It processes the output of g++ -v --help (silencing the extra info it prints to stderr), matches lines that start with -std= then captures the values. The ^< is to block the -std=<standard> line of the help. Here is some example output for GCC 9:

f2003
f2008
f2008ts
f2018
f95
gnu
legacy
c++03
c++0x
c++11
c++14
c++17
c++1y
c++1z
c++2a
c++98
c11
c17
c18
c1x
c2x
c89
c90
c99
c9x
gnu++03
gnu++0x
gnu++11
gnu++14
gnu++17
gnu++1y
gnu++1z
gnu++2a
gnu++98
gnu11
gnu17
gnu18
gnu1x
gnu2x
gnu89
gnu90
gnu99
gnu9x
iso9899:1990
iso9899:199409
iso9899:1999
iso9899:199x
iso9899:2011
iso9899:2017
iso9899:2018

You can add a grep in the middle to filter based on help description text, which is conveniently consistent in the help output. E.g. if you want to drop the deprecated ones:

gcc -v --help 2> /dev/null | grep -iv deprecated | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

If you want to list just non-deprecated C++:

gcc -v --help 2> /dev/null | grep -iv deprecated | grep "C++" | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

If you want to list just non-deprecated C:

gcc -v --help 2> /dev/null | grep -iv deprecated | grep "C " | sed -n '/^ *-std=\([^<][^ ]\+\).*/ {s//\1/p}'

Those are pretty hacky and rely on "deprecated", "C++" and/or "C" (note the space at the end of grep "C "!) appearing in the help description for each standard name but they seem to work.

You could similarly filter out e.g. "same as" to get rid of synonymous ones, etc.

Solution 2

This information is available on GCC official website. Here are the relevant tables:

C++11 features support

C++14 features support

C++17 features support

C++20 features support

Solution 3

The comment that @oldMammuth made is almost correct, gcc and g++ do actually have a way to print the language standards they supports. It's just not well documented. I'd say it was basically hidden if it wasn't listed in parentheses below the help text of the --help argument. The way it's done is by going through the GNU compiler toolchain and asking the particular compiler instance you're using for it's --help text. I actually just learned this after having to do this research for my own project, but in order to bundle a whole bunch of compilers under one program, gcc and g++ do exactly that, they use the main executable to serve as a middleman for communicating with the compiler, assembler, and linker processes. In order to access the help text to get the supported language standards of a given compiler version, you have to ask the compiler, not gcc or g++.

Since at this point, I'm getting tired of typing both commands, the rest of this will assume we're using gcc; despite the fact that both commands are virtually interchangeable and basically the same middleman with a different name.

You can get the path to said compiler by using gcc -print-prog-name=cc1. On my system this is /usr/lib/gcc/x86_64-linux-gnu/8/cc1. Then, just call said executable with the --help parameter, and you're all set! Beware, there are hundreds of help parameter entries. I actually recommend piping the output through grep and using a regex to find them because otherwise there's so much extra information it's really annoying.

ALTERNATIVELY:

You can use gcc -v --help as stated in the gcc help text, to print the help dialogues for each program in the given tool-chain. This does result in a lot more output however.

Again, my recommendation is to use a regex to search through the output and find the standard versions supported. gcc also supports more languages than C and C++, including but not limited to, Fortran and Go.

Solution 4

gcc and g++ do not have a command line option to check this out. It would be nice that the -v option would tell something about the supported standards. Instead you can check the online docs at gcc Standards and the useful synopsis at cppreference.com.

According to cppreference, full support of c++11 came with gcc 4.8.1;

To have full support of c++14 (with some of the new features of c++17), instead, you need gcc 5.0 and above.

Share:
70,920

Related videos on Youtube

Šimon Hrabec
Author by

Šimon Hrabec

Updated on August 20, 2021

Comments

  • Šimon Hrabec
    Šimon Hrabec over 2 years

    How do I find which standards my GCC compiler supports? I don't mean how do I find out at the compilation time what C++ standard is being used (checking defined constants), but before compiling, how can I check available standards to use (i.e. for flag -std=c++??)?

    The information is not present in man g++.

    I can check out my GCC version by g++ --version besides manually trying the options?

    Is it possible to find somewhere table of GCC versions and supported standards?

  • rm1948
    rm1948 over 2 years
    A one line version: $(gcc -print-prog-name=cc1) --help | grep std
  • MRL
    MRL about 2 years
    Nice! Very helpful