97

I'm trying to compile using g++ and either the -std=c++11 or c++0x flags.

However, I get this error

cc1plus: error: unrecognized command line option "-std=c++11"

g++ --version

g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

4 Answers4

107

Seeing from your G++ version, you need to update it badly. C++11 has only been available since G++ 4.3. The most recent version is 4.7.

In versions pre-G++ 4.7, you'll have to use -std=c++0x, for more recent versions you can use -std=c++11.

antonijn
  • 5,702
  • 2
  • 26
  • 33
14

Quoting from the gcc website:

C++11 features are available as part of the "mainline" GCC compiler in the trunk of GCC's Subversion repository and in GCC 4.3 and later. To enable C++0x support, add the command-line parameter -std=c++0x to your g++ command line. Or, to enable GNU extensions in addition to C++0x extensions, add -std=gnu++0x to your g++ command line. GCC 4.7 and later support -std=c++11 and -std=gnu++11 as well.

So probably you use a version of g++ which doesn't support -std=c++11. Try -std=c++0x instead.

Availability of C++11 features is for versions >= 4.3 only.

stefan
  • 10,215
  • 4
  • 49
  • 90
  • @Antonijn Correct, but not an option for everyone. – stefan Feb 03 '13 at 16:32
  • @Antonijn: There is software which is incompatible with versions > 4.1. E.g. ABACUS http://www.informatik.uni-koeln.de/abacus/index.html Sadly enough I had to use it once. – stefan Feb 03 '13 at 16:35
3

you should try this

g++-4.4 -std=c++0x or g++-4.7 -std=c++0x
codegeek
  • 32,236
  • 12
  • 63
  • 63
papo
  • 155
  • 1
  • 6
-4

I also got same error, compiling with -D flag fixed it, Try this:

g++ -Dstd=c++11

pankaj kushwaha
  • 369
  • 5
  • 20
  • 1
    Why is this downvoted? What's wrong with the answer? Why doesn't this work? – Veda Nov 24 '16 at 12:32
  • @Veda for me at least (on gcc4.6.3), it definitely was not compiling with c++11. I wrote a simple 2 line program with `std::vector v = {1, 2, 3};` to test and it was throwing all sorts of errors over it (different errors than without the flag though, interestingly enough). – scohe001 Jan 23 '18 at 22:05
  • 8
    @Veda while this answer will compile, it very likely doesn't do anything desirable. The `-D` command line argument is equivalent to inserting a `#define` in your source code. So this command is like having `#define std c++11`. Can you then imagine how `std::string` will be redefined to `c++11::string`? Not very useful at all. – jwm Mar 13 '18 at 17:43