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.
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.
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
Your ubuntu definitely has a sufficiently recent version of g++. The flag to use is -std=c++0x
.
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.
You can refer to following link for which features are supported in particular version of compiler. It has an exhaustive list of feature support in compiler. Looks GCC follows standard closely and implements before any other compiler.
Regarding your question you can compile using
g++ -std=c++11
for C++11
g++ -std=c++14
for C++14
g++ -std=c++17
for C++17
g++ -std=c++2a
for C++20, although all features of C++20 are not yet supported refer this link for feature support list in GCC.
The list changes pretty fast, keep an eye on the list, if you are waiting for particular feature to be supported.