165

What are the differences between -std=c++11 and -std=gnu++11 as compilation parameter for gcc and clang? Same question with c99 and gnu99? I know about C++ and C standards, it's the differences in the parameters that interest me.

I've read somewhere that it has to do with some extensions but it is not clear to me which ones and how to choose between one or the other for a new project.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157
Klaim
  • 67,274
  • 36
  • 133
  • 188
  • 7
    The `gnu` ones do refer to extensions, and if you want to write portable code you should stick to a standard and avoid extensions altogether. – juanchopanza May 16 '12 at 06:49

1 Answers1

110

As you have found out yourself, the difference between the two options is whether GNU extensions that violates/extend the C++ standard are enabled or not. The GNU C++ extensions are described here. You can also use most of the GNU C extensions (described here) in your C++ programs. It would be also useful to read about the -Wpedantic GCC option here.

Note that some extensions can still be in effect when using -std=c++11, as long as they do not contradict the standard. For instance, when using the MinGW compiler, I need the extensions for a working Boost.Lexical_Cast. But, as long as you don't use any of them, you are better off sticking to the standard without extensions for maximum portability. This might come in handy if you find yourself forced to change compiler.

daramarak
  • 6,115
  • 1
  • 31
  • 50
  • 6
    Yeah, I avoid extensions because I don't recommend doing anything that isn't specifically defined by the Standard... but even then, "violates" is a strange and loaded term, when a lot of these extensions are, to use Standardese, just _implementation-defining_ or _specifying_ things that the Standard is silent on - or perhaps even all of the extensions... Do you have a citation for any GNU extension that violates the Standard by doing something different from a thing the Standard explicitly defines? – underscore_d Jul 05 '16 at 00:27
  • 12
    While this answer is a good general one, would you consider editing it so as to at least list the extensions which are enabled in `gnu11` but not in `c++11`? The list you linked to is of all extensions, and as you yourself indicate some of them are enabled with `c++11` as well (like `__restrict__`). – einpoklum Oct 04 '16 at 08:16
  • What I found out now, and would never concluded by looking at the documentation of the flags, is the following: -std=c++11 re-enables trigraphs -std=gnu++11 ignores them and emits a warning, like it does when omitting -std alltogether – Daniel82 Mar 24 '20 at 10:52
  • 1
    The best example still valid in 2022 until 2024 with Centos 7, is the `` library. Compiling with `--std=c++11` works but the `regex_match()` will always `return false;`. People waste hours debugging something that isn't actually implemented. – Alexis Apr 27 '22 at 02:45