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.
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.
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.
you should try this
g++-4.4 -std=c++0x or g++-4.7 -std=c++0x
I also got same error, compiling with -D flag fixed it, Try this:
g++ -Dstd=c++11
Related
is that possible that g++ somehow compile my program with older standard than I specified?
I compile with:
g++ -Wall -Wextra -pedantic -O3 -std=c++2a -fconcepts
And compiler can't recognize bind_front function ( I included <functional> ). Compiler version is GCC 8.3.
GCC 8.3 does not support std::bind_front. Check here.
You need to use GCC 9.1 or 9.2. Check here.
How to install GCC 9?
UPDATE
As the #walnut's comment says, there is a g++ 9 package in the standard repositories since Ubuntu 19.04.
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
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
g++ source_file.cpp -o executable_name -std=c++11 for C++11
g++ source_file.cpp -o executable_name -std=c++14 for C++14
g++ source_file.cpp -o executable_name -std=c++17 for C++17
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.
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.
Use -std=c++11 compiler flag for ISO C++11.
For more details on C++ compiler flags and options, check this.
I'm trying to configure eclipise kepler to use c++ 11.
I appended -std=c++11 to:
Properties > c/c++ build > settings > GCC c++ complier > Miscellaneous>other flags
But when I compile the project it says:
compilation terminated. /bin/sh: 1: -std=c++11: not found
I'm using gcc on ubuntu,
any ideas?
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++ -Wall -g -std=c++11 your_file.cpp -o your_program
or
$ g++ -Wall -g -std=c++0x your_file.cpp -o your_program
if the above doesn't work.
So in your case if -std=c++11 does not work, try -std=c++0x
Another source for this error could be an old compiler version.
Type gcc --version into the terminal and check the version. Here is a quick overview which version is capable of c++11:
C++11 Support in GCC
GCC 4.8.1 was the first feature-complete implementation of the 2011
C++ standard, previously known as C++0x.
This mode can be selected with the -std=c++11 command-line flag, or
-std=gnu++11 to enable GNU extensions as well.
Link: https://gcc.gnu.org/projects/cxx-status.html
When is it necessary to use use the flag -stdlib=libstdc++ for the compiler and linker when compiling with gcc?
Does the compiler automatically use libstdc++?
I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard. I already pass -std=c++11 to the compiler.
On Linux: In general, all commonly available linux distributions will use libstdc++ by default, and all modern versions of GCC come with a libstdc++ that supports C++11. If you want to compile c++11 code here, use one of:
g++ -std=c++11 input.cxx -o a.out (usually GNU compiler)
g++ -std=gnu++11 input.cxx -o a.out
On OS X before Mavericks: g++ was actually an alias for clang++ and Apple's old version of libstdc++ was the default. You could use libc++ (which included c++11 library support) by passing -stdlib=libc++. If you want to compile c++11 code here, use one of:
g++ -std=c++11 -stdlib=libc++ input.cxx -o a.out (clang, not GNU compiler!)
g++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out (clang, not GNU compiler!)
clang++ -std=c++11 -stdlib=libc++ input.cxx -o a.out
clang++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out
On OS X since Mavericks: libc++ is the default and you should not pass any -stdlib=<...> flag. Since Xcode 10, building against libstdc++ is not supported at all anymore. Existing code built against libstdc++ will keep working because libstdc++.6.dylib is still provided, but compiling new code against libstdc++ is not supported.
clang++ -std=c++11 input.cxx -o a.out
clang++ -std=gnu++11 input.cxx -o a.out
When is it necessary to use use the flag -stdlib=libstdc++ for the compiler and linker when compiling with gcc?
Short answer: never
Longer answer: -stdlib is a Clang flag and will not work with any version of GCC ever released. On macOS sometimes the gcc and g++ commands are actually aliases for Clang not GCC, and the version of libstdc++ that Apple ships is ancient (circa 2008) so of course it doesn't support C++11. This means that on macOS when using Clang-pretending-to-be-GCC, you can use -stdlib=libc++ to select Clang's new C++11-compatible library, or you can use -stdlib=libstdc++ to select the pre-C++11 antique version of libstdc++ that belongs in a museum. But on GNU/Linux gcc and g++ really are GCC not Clang, and so the -stdlib option won't work at all.
Edit: Since I wrote this answer, GCC was changed to conditionally support the -stdlib flag, but for most platforms that support is disabled by default. Even when it's enabled, the default is -stdlib=libstdc++ so you still never need to say that explicitly. GCC will still automatically use libstdc++.
Does the compiler automatically use libstdc++?
Yes, GCC always uses libstdc++ unless you tell it to use no standard library at all with the -nostdlib option (in which case you either need to avoid using any standard library features, or use -I and -L and -l flags to point it to an alternative set of header and library files).
I am using gcc4.8.2 on Ubuntu 13.10 and I would like to use the c++11 standard. I already pass -std=c++11 to the compiler.
You don't need to do anything else. GCC comes with its own implementation of the C++ standard library (libstdc++) which is developed and tested alongside GCC itself so the version of GCC and the version of libstdc++ are 100% compatible. If you compile with -std=c++11 then that enables the C++11 features in g++ compiler and also the C++11 features in the libstdc++ headers.
The compiler uses the libstdc++ automatically, if you use the g++ frontend, not the gcc frontend.
By default, on Ubuntu, when I try to compile with g ++ like:
g++ -o name file.cpp
Sometimes it responds with "ISO C++ forbids..."
If I don't want to compile with ISO, what flag should I use?
use
g++ -std=c++0x
or
g++ -std=gnu++0x
Note: If you proprietary enable extensions, your code will become less portable.
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.
See http://gcc.gnu.org/projects/cxx0x.html
If you want to use gcc/g++ extensions, then use -std=gnu++0x. Assuming of course it's not a case of "you can't actually do this in C++".