167

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.

1
  • 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. Commented May 16, 2012 at 6:49

1 Answer 1

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.

4
  • 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? Commented Jul 5, 2016 at 0: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
    Commented Oct 4, 2016 at 8: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
    Commented Mar 24, 2020 at 10:52
  • 1
    The best example still valid in 2022 until 2024 with Centos 7, is the <regex> 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.
    – None
    Commented Apr 27, 2022 at 2:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.