425

I'm trying to update my C++ compiler to C++11. I have searched a bit and I have come to the conclusion that I have to use the flag -std=c++0x or -std=gnu++0x, but I don't know many things about flags. Can anyone help me? (I'm using Ubuntu 12.04.)

Here is the error that I get from the compiler when I attempt to use a library which is included in C++11 (i.e. array):

#include <array>
#include <iostream>

int main()
{
    std::array<int, 3> arr = {2, 3, 5};
    ...
}

This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

nbro
  • 15,395
  • 32
  • 113
  • 196
Rontogiannis Aristofanis
  • 8,883
  • 8
  • 41
  • 58
  • It depends on what you are using to build. Makefile? Eclipse? Something else? – dutt Apr 28 '12 at 12:54
  • 6
    In the newest version, you probably have to use `-std=c++11` instead. Maybe both are allowed, though. –  Apr 28 '12 at 12:56
  • 6
    This misses a lot of context. Show the full command you've tried (maybe even sample code you tried to compile), and what actually failed. – KillianDS Apr 28 '12 at 12:56
  • 5
    @classdaknok_t: both should be allowed, also ubuntu 12.04 ships `g++-4.6` by default (which only supports `-std=c++0x`) – KillianDS Apr 28 '12 at 12:57
  • I use `g++ _filename_ && ./a.out` – Rontogiannis Aristofanis Apr 28 '12 at 13:10
  • 3
    Add flags right after `g++`, e.g. `g++ -std=c++0x _filename_ && ./a.out`. – n. m. could be an AI Apr 28 '12 at 13:23
  • You can check the man page for a complete list (just search for `/c11` to find the section describing all the flags, you might have to press `n` a few times to skip to the next search results), but here's a few of the more common values to pass into the `-std=` flag: -std=c++98, -std=c++03, -std=c++11, -std=c++14, -std=gnu++98, -std=gnu++03, -std=gnu+=11, -std=gnu++14 – MikeyE Nov 16 '16 at 05:40
  • 2
    Came across this post, searching for titled error message. Im working on a QMAKE project, solution for me was adding CONFIG += c++11 to the .pro file. –  Jan 12 '17 at 20:35

6 Answers6

616

Flags (or compiler options) are nothing but ordinary command line arguments passed to the compiler executable.

Assuming you are invoking g++ from the command line (terminal):

$ g++ -std=c++11 your_file.cpp -o your_program

or

$ g++ -std=c++0x your_file.cpp -o your_program

if the above doesn't work.

Oskar N.
  • 8,427
  • 2
  • 23
  • 21
  • 11
    Don't forget to put `-Wall -g` just after `g++ ` – Basile Starynkevitch Apr 28 '12 at 13:41
  • 7
    @BasileStarynkevitch: I would add `-Werror` too, no reason not to when starting a project. – Matthieu M. Apr 28 '12 at 14:14
  • @Matthieu M.: While we are it, replace g++ with clang++ as well, for better C++11 support, superior error diagnostics, and sane warning options turned on by default. ;-) – Oskar N. Apr 29 '12 at 16:17
  • @OskarN.: I personally use Clang (ToT) for personnal projects :) – Matthieu M. Apr 29 '12 at 16:53
  • 3
    Better C++11 support is debatable and better diagnostics don't look so much better these days (Clang's page describing them uses GCC 4.2 which is ancient) http://gcc.gnu.org/wiki/ClangDiagnosticsComparison ;) – Jonathan Wakely May 18 '12 at 17:52
  • 61
    Does anyone know if/when C++ compilers will support the C++11 standard by default, that is, without a flag? – Dennis Apr 06 '13 at 07:17
  • 2
    And in case you don't already know, in Visual C++ simply use VC11 (Visual Studio 2012) or above to have C++11 features – gerrytan Dec 04 '14 at 02:40
  • 1
    @gerrytan Visual C++ doesn't support control over C++ standard so you always get all C++ features that you version of vs provides. This is a problem when doing cross platform development and one platform has C++ standard limitation. In such case you have to remember which feature you can use and which you are not allowed to use, since it is not available on other platform. – Marek R Oct 31 '17 at 23:57
  • 3
    @Dennis in the meantime, you can use an alias in your bash_profile: `alias gpp="g++ -std=c++11"` – Paul Razvan Berg Jun 28 '18 at 11:58
  • @OskarN. Can you explain what 0x means? – Nike Aug 30 '19 at 15:43
  • @user1271772 C++0x was the working name for what was going to be the C++11 standard when the standards committee still thought they could release it before the year 2010, so 0x refers to the last two digits in the year of the release. Much like C++14 was called C++1y, where y=4 but they actually made it before the year 2020. – Oskar N. Sep 27 '19 at 20:05
50

You can check your g++ by command:

which g++
g++ --version

this will tell you which complier is currently it is pointing.

To switch to g++ 4.7 (assuming that you have installed it in your machine),run:

sudo update-alternatives --config gcc

There are 2 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path              Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-4.6   60        auto mode
  1            /usr/bin/gcc-4.6   60        manual mode
* 2            /usr/bin/gcc-4.7   40        manual mode

Then select 2 as selection(My machine already pointing to g++ 4.7,so the *)

Once you switch the complier then again run g++ --version to check the switching has happened correctly.

Now compile your program with

g++ -std=c++11 your_file.cpp -o main
Christoph
  • 47,569
  • 8
  • 87
  • 187
Harajyoti Das
  • 721
  • 5
  • 12
  • Might be worth mentioning that versions like g++ 4.4 might be simply too old to do some features, so upgrading is necessary. Upgrading will depend on your system, and I'd avoid methods of compiling g++ yourself and replacing the system's compiler for reference – Colin D Aug 30 '16 at 18:07
27

You can refer to following link to know which features are supported in which version of compiler. It has an exhaustive list of feature support in modern compilers. Seems like GCC follows the standard very closely and implements before any other compiler.

Regarding your question, you can compile using

  1. g++ source_file.cpp -o executable_name -std=c++11 for C++11
  2. g++ source_file.cpp -o executable_name -std=c++14 for C++14
  3. g++ source_file.cpp -o executable_name -std=c++17 for C++17
  4. g++ source_file.cpp -o executable_name -std=c++2a for C++20, All the features of C++20 are not yet supported. Refer to this link for feature support list in GCC.

The list changes pretty fast, keep an eye on the list, if you are waiting for a particular feature to be supported.

yadhu
  • 1,253
  • 14
  • 25
16

Your Ubuntu definitely has a sufficiently recent version of g++. The flag to use is -std=c++0x.

Guy Avraham
  • 3,482
  • 3
  • 38
  • 50
Michael Slade
  • 13,802
  • 2
  • 39
  • 44
13

If you want to keep the GNU compiler extensions, use -std=gnu++0x rather than -std=c++0x. Here's a quote from the man page:

The compiler can accept several base standards, such as c89 or c++98, and GNU dialects of those standards, such as gnu89 or gnu++98. By specifying a base standard, the compiler will accept all programs following that standard and those using GNU extensions that do not contradict it. For example, -std=c89 turns off certain features of GCC that are incompatible with ISO C90, such as the "asm" and "typeof" keywords, but not other GNU extensions that do not have a meaning in ISO C90, such as omitting the middle term of a "?:" expression. On the other hand, by specifying a GNU dialect of a standard, all features the compiler support are enabled, even when those features change the meaning of the base standard and some strict-conforming programs may be rejected. The particular standard is used by -pedantic to identify which features are GNU extensions given that version of the standard. For example-std=gnu89 -pedantic would warn about C++ style // comments, while -std=gnu99 -pedantic would not.

  • And what does that get you, aside from the binary `?:` operator? The only other extension that comes to mind, structure expressions, is superceded by C++11 list initialization. In any case, this quote mainly relates to C, not C++. – Potatoswatter Apr 28 '12 at 14:06
4

Use -std=c++11 compiler flag for ISO C++11.
For more details on C++ compiler flags and options, check this.

Dada
  • 6,313
  • 7
  • 24
  • 43
Sadeesh Kalhara
  • 129
  • 1
  • 7