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
or -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.
-stdlib=libstdc++
is not a valid gcc flag. It is usable on MacOS only becauseg++
on MacOS is actuallyclang++
. – Retharethink