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.
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.
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 Mac OS X 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 OS X 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.
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.
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 taking part in a programming contest and the requirement is that code will be compiled using following command:
g++ -std=c++11 -O2 -o a.out orienteering.cpp
How do I check if my code works for this command? (I use DevC++ for coding and it has automatic compilation).
Also compiler should be GCC 4.8.2 or later. What does this mean? Is my older GCC version (4.7.2) not suitable?
You check your code by placing it in a file named orienteering.cpp, and running this command in the same directory:
g++ -std=c++11 -O2 -o a.out orienteering.cpp
If the compiler spits out any messages at all then you have a problem. If the compiler is silent and creates a file named a.out, then all is well.
GCC 4.7.2 does not meet the criteria "GCC 4.8.2 or later".
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++".