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
g++ -std=gnu++11 input.cxx -o a.out
On OS X before Mavericks:
g++
was actually an alias forclang++
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
g++ -std=gnu++11 -stdlib=libc++ input.cxx -o a.out
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. You can use Apple's old version of libstdc++ (which does not include c++11 library support) by passing
-stdlib=libstdc++
clang++ -std=c++11 input.cxx -o a.out
clang++ -std=gnu++11 input.cxx -o a.out
The compiler uses the libstdc++ automatically, if you use the g++ frontend, not the gcc frontend.
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 thegcc
andg++
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/Linuxgcc
andg++
really are GCC not Clang, and so the-stdlib
option won't work at all.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).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 ing++
compiler and also the C++11 features in the libstdc++ headers.