Which C++ Standard Is the Default When Compiling with G++

How to determine what C++ standard is the default for a C++ compiler?

What about compiling and executing the following trivial program ?

#include <iostream>

int main()
{ std::cout << __cplusplus << std::endl; }

The value printed should say the version used:

  • 199711 for C++98,
  • 201103 for C++11
  • 201402 for C++14
  • 201703 for C++17

If you compile omitting the -std=c++xx flag, you should be able to detect the default version of language used.

Is g++ different than g++ -std=c++14 when the default standard is 201402L (c++14)?

GCC doesn't compile with -std=c++14 by default. The description of the -std flag from the GCC man pages (for version 9.3.0) says

-std= Determine the language standard. This option is currently only supported when compiling C or C++.

The compiler can accept several base standards, such as c90 or c++98, and GNU dialects of those standards, such as gnu90 or gnu++98. When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it.

. . .

A value for this option must be provided; possible values are

. . .

c++14

c++1y

    The 2014 ISO C++ standard plus amendments. The name c++1y is deprecated.



gnu++14

gnu++1y

    GNU dialect of -std=c++14. This is the default for C++ code. The name gnu++1y is deprecated.

. . .

Emphasis mine. The current default is -std=gnu++14, which targets the C++14 standard while also enabling GNU extensions to the C++ language. The distinction between the -std=c++XX flags and the -std=gnu++XX flags is explained further in What are the differences between -std=c++11 and -std=gnu++11?.

What is the default C -std standard version for the current GCC (especially on Ubuntu)?

This is explained in depth in the gcc manual, available (if it's installed) by typing info gcc or online here. The relevant section of the 4.7.2 manual is here.

By default, gcc does not conform to any of the ANSI/ISO C standards. The current default is equivalent to -std=gnu90, which is the 1989/1990 standard with GNU-specific extensions. (Some diagnostics required by the language standard are not issued.) Version 5.1.0, released 2015-04-22, changed the default from -std=gnu90 to -std=gnu11, as documented here.

If you want standard conformance, you can use any of the following:

-std=c90 -pedantic
-std=c99 -pedantic
-std=c11 -pedantic

-std=c90 can also be spelled -ansi, -std=c89, or -std=iso9899:1990.

-std=iso9899:199409 supports the C90 standard plus the 1995 amendment, which added a few minor features (all of which are also in C99).

-std=c99 can also be spelled -std=c9x or -std=iso9899:1999 (the name c9x was used before the standard was published). C99 support is not quite complete, but it's close.

-std=c11 can also be spelled -std=c0x or -std=iso9899:2011 (the name c0x was used before the final standard was published; it was wrongly assumed that x would not exceed 9). C11 support is also incomplete; the current status is summarized here.

The -pedantic option causes gcc to print required diagnostics for violations of constraints and syntax rules. In some cases, those diagnostics are merely warnings -- and there's no easy way to distinguish between those warnings and other warnings that aren't required by the language. Replace -pedantic by -pedantic-errors to cause gcc to treat language violations as fatal errors.

A quick history of the standard:

  • C89 was the first official C standard, published by ANSI in 1989.
  • C90 was the ISO version of the standard, describing exactly the same language as C89. ANSI officially adopted ISO's version of the standard. There were two Technical Corrigenda, correcting some errors.
  • C95 was an amendment to C90, adding a few features, mainly digraphs and wide character support. As far as I know, a merged version was never published.
  • C99 was issued by ISO in 1999. There were three Technical Corrigenda.
  • C11 was issued by ISO in 2011. There has been one Technical Corrigendum, fixing the definitions of __STDC_VERSION__ and __STDC_LIB_EXT1__.

ANSI did not issue its own versions of the 1999 or 2011 standards, adopting the ISO standards instead.

N1256 is a freely available draft of the C99 standard, with the 3 Technical Corrigenda merged into it.

N1570 is a freely available draft of the C11 standard. There are some minor differences between it and the published C11 standard, plus one Technical Corrigendum. For more details, see my answer to this question.

how to define -std=c++11 as default in g++

Yes, you typically set this in a Makefile:

CXXFLAGS=-std=c++11 

One layer above you can also detect a suitable compiler via autoconf, cmake or whichever other meta-buildtool you might deploy.

You of course play games as define g++11 as g++ -std=c++11 but such set-ups are not portable.

g++-6.* will default to c++14 so at some this switch will be implicit. But it might take a really long time for all those RHEL and CentOS boxen with g++-4.4.* to disappear. Those may not even handle your current project...

how to configure -std=c++11 as default compiler?

as stated here, the only way to change the standard version is to rebuild a custom version of g++. If you are using Linux, I recommend having a custom alias in .bashrc, like so:

alias g++11='g++ -std=c++11';

CMake is another common method to do this, simply add this directive to make the default version C++11 :

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

Keep in mind CMake is typically used for projects, so it may not be applicable to your use case.



Related Topics



Leave a reply



Submit