Compiling C++11 with g++

C++C++11G++Flags

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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

Solution 3 - C++

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.

Solution 4 - C++

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

Solution 5 - C++

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.

Solution 6 - C++

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRontogiannis AristofanisView Question on Stackoverflow
Solution 1 - C++Oskar N.View Answer on Stackoverflow
Solution 2 - C++Harajyoti DasView Answer on Stackoverflow
Solution 3 - C++yadhuView Answer on Stackoverflow
Solution 4 - C++Michael SladeView Answer on Stackoverflow
Solution 5 - C++user1356386View Answer on Stackoverflow
Solution 6 - C++Sadeesh KalharaView Answer on Stackoverflow