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
Answer from Bill Lynch on Stack Overflow
Top answer
1 of 3
120

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
2 of 3
39

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.

🌐
GitHub
github.com › rstudio › sass › issues › 120
-stdlib=libc++ should be conditional on compiler and not hardcoded on macOS · Issue #120 · rstudio/sass
December 7, 2022 - These flags in Makefile should be conditional on compiler and not just OS: ifeq ($(UNAME),Darwin) CFLAGS += -stdlib=libc++ CXXFLAGS += -stdlib=libc++ LDFLAGS += -stdlib=libc++ endif Otherwise build with GCC is broken: /opt/local/bin/gcc-...
Author   rstudio
Discussions

Installing Spacy on Mac - "gcc: error: unrecognized command line option '-stdlib=libc++'"
If you're a mac user with gcc/g++ (e.g. a brew-installed version) rather than clang as your default compiler, then you may/will probably hit this error on install - gcc: error: unrecognized command line option '-stdlib=libc++. More on github.com
🌐 github.com
8
January 27, 2016
gcc - Specify which libstdc++ to use - Unix & Linux Stack Exchange
I want to make use of a more recent GCC version. Therefore, I compiled GCC 10 and installed it into a non-standard directory /software/gcc10/. So far so good. However, I am faced with problems when I More on unix.stackexchange.com
🌐 unix.stackexchange.com
March 16, 2022
Unrecognized command line option '-stdlib=libc++ for certain root gcc combinations on MAC
Dear rooters, I’m working on a MACBook, OSX V 10.8.5. I’m trying to compile root and subsequently a program (see attached Makefile) against root. I use various versions of gcc (installed via macports) and root. I just compile root without any special configuration options -use gcc 4.7 -compile ... More on root-forum.cern.ch
🌐 root-forum.cern.ch
0
0
September 18, 2015
Unintended Consequences of Supporting -stdlib=libc++
In the past, Darwin shipped an ancient C++ standard library by default, so most C++ programs needed to be compiled with the option stdlib=libc++ to use the newer library shipped on macOS. This option is only supported by clang on Darwin, and it's invalid for GCC. More on github.com
🌐 github.com
23
April 27, 2023
🌐
GitHub
github.com › explosion › spaCy › issues › 237
Installing Spacy on Mac - "gcc: error: unrecognized command line option '-stdlib=libc++'" · Issue #237 · explosion/spaCy
January 27, 2016 - If you're a mac user with gcc/g++ (e.g. a brew-installed version) rather than clang as your default compiler, then you may/will probably hit this error on install - gcc: error: unrecognized command line option '-stdlib=libc++.
Author   explosion
🌐
CERN
root-forum.cern.ch › cling
Unrecognized command line option '-stdlib=libc++ for certain root gcc combinations on MAC - Cling - ROOT Forum
September 18, 2015 - Dear rooters, I’m working on a MACBook, OSX V 10.8.5. I’m trying to compile root and subsequently a program (see attached Makefile) against root. I use various versions of gcc (installed via macports) and root. I just compile root without any special configuration options -use gcc 4.7 -compile root 5.34 with it ->compile the program against root works -use gcc 4.8 or 4.9 -compile root 6.05.02 with it -> compile the program against root gives following error: c++: error: unrecognized ...
🌐
GNU
gcc.gnu.org › pipermail › gcc › 2022-April › 238479.html
-stdlib=libc++? - GCC, the GNU Compiler Collection
April 2, 2022 - Hi Iain, Thank you for the quick response and the effort to make that feature available. When I reconfigured/build GCC with --with-gxx-libcxx-include-dir=/usr/include/c++/v1/ , -stdlib= option is now available to take libc++. thanks, Shivam.
Find elsewhere
🌐
LLVM
releases.llvm.org › 5.0.0 › projects › libcxx › docs › UsingLibcxx.html
Using libc++ — libc++ 5.0 documentation
$ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc · Alternately, you could just add libc++abi to your libraries list, which in most situations will give the same result: ... GCC does not provide a way to switch from libstdc++ to libc++. You must manually configure the ...
🌐
Blogger
shannon112.blogspot.com › 2021 › 07 › c-compiler-and-standard-library-gcc.html
c++ compiler and standard library ( GCC & libstdc++, LLVM & libc++ ~ shannon112
July 17, 2021 - clang++ test.cpp -std=c++17 clang++ test.cpp -stdlib=libc++ clang++ with libstdc++ clang++ test.cpp -stdlib=libstdc++ https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc · Print the lists of directories and candidate libraries stored in the current cache https://linux.die.net/man/8/ldconfig ·
🌐
LLVM
releases.llvm.org › 4.0.0 › projects › libcxx › docs › UsingLibcxx.html
Using libc++ — libc++ 4 documentation
$ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc · Alternately, you could just add libc++abi to your libraries list, which in most situations will give the same result: ... GCC does not provide a way to switch from libstdc++ to libc++. You must manually configure the ...
🌐
GNU
gcc.gnu.org › pipermail › gcc › 2022-April › 238482.html
-stdlib=libc++?
April 3, 2022 - For GCC, the default is to use -stdlib=libstdc++, and that is part of the compiler’s install so that it can be located without extra configuration, and it does not require the -stdlib option to work. > While in clang equivalent, -stdlib= doesn't require so. libc++ is the default for clang ...
🌐
GitHub
github.com › iains › gcc-12-branch › issues › 21
Unintended Consequences of Supporting -stdlib=libc++ · Issue #21 · iains/gcc-12-branch
April 27, 2023 - In the past, because -stdlib=libc++ was unsupported, the build system would remove -stdlib=libc++ from the CFLAGS and successfully building the program. But now, -stdlib=libc++ is passed to GCC, the compilation of the same program now fails due to missing libc++ system headers.
Author   iains
🌐
OSDev Wiki
wiki.osdev.org › GCC_and_Libc++
GCC and Libc++ - OSDev Wiki
Note that you might also want to configure GCC to use multilib support so that you can turn off the red zone with respect to x86_64 Libgcc_without_red_zone. The code in this tutorial assumes that you have done that as it's pretty much needed if you want to run code in your kernel using x86_64. Finally you will also need a libc as libc++ uses libc functions.
🌐
LLVM
releases.llvm.org › 3.9.0 › projects › libcxx › docs › UsingLibcxx.html
Using libc++ — libc++ 3.9 documentation
$ clang++ -stdlib=libc++ test.cpp -lc++ -lc++abi -lm -lc -lgcc_s -lgcc · Alternately, you could just add libc++abi to your libraries list, which in most situations will give the same result: ... GCC does not provide a way to switch from libstdc++ to libc++. You must manually configure the ...
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++
The GNU C++ Library
Short Contents Copyright (C) 2008-2026 FSF Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover ...
🌐
CERN
root-forum.cern.ch › t › stdlib-libc-unrecognized › 18933
Stdlib=libc++ unrecognized - Page 2 - ROOT - ROOT Forum
March 17, 2015 - Greetings, Switching the order of the GLIBS etc did not change anything. However I was able to solve this issue for the Mac OSX 10.10 and 10.8. It appears that the issue is related back to the -stdlib=libc++ in g++. So using the tip you gave previously I changed the compiler to clang++, i.e. [code]ifeq ($(ARCH),macosx64) Mac OSX CXX = clang++ CXXFLAGS = -O -Wall -fPIC LD = clang++ LDFLAGS = -O SOFLAGS = -shared endif[/code] with this I was able to...
🌐
GitHub
github.com › geany › geany › issues › 1618
Need to add -stdlib=libc++ flag on OS X because of Scintilla's C++11 requirement · Issue #1618 · geany/geany
September 29, 2017 - The problem on OS X is that there are two C++ standard libraries - libstdc++ (legacy without C++11 support) and libc++ (supported one with C++11). When llvm is invoked as gcc in the gcc compatibility mode (which happens when building Geany), libstdc++ is used by default which leads to the compilation errors because of missing C++11 support. For this reason I'd need to add -stdlib=libc++ flag on the Mac build.
Author   geany
🌐
CERN
root-forum.cern.ch › t › stdlib-libc-unrecognized › 18933
Stdlib=libc++ unrecognized - ROOT - ROOT Forum
March 9, 2015 - Greetings, I recently migrated to OSX 10.10 Yosemite. Installed ROOT 5.34.26 via Homebrew. When compiling code that compiled correctly under OSX 10.8 Mountain Lion, ROOT 5.43.00, I get an error of I use a Makefile for compiling, and the g++ command out of the Makefile for OSX 10.10 is The g++ command from OSX 10.8 is If I copy the OSX 10.10 g++ output and paste without the -stdlib=libc++, it compiles correctly.
🌐
Reddit
reddit.com › r/cpp_questions › some questions about libc, libc++, libstdc++
r/cpp_questions on Reddit: Some questions about libc, libc++, libstdc++
September 18, 2020 -

Can someone elaborate on what are libc, libc++, libstdc++

  1. What the difference between libc++ and libstdc++? Why can't we just use one of them for all times?

  2. How to determine which versions of these libs support specific standard version? For example which version of c++ standard does libstdc++-4.6 support?

  3. Can i just "copy-paste" these libs into target OS to run my project if that OS lacks newer versions of these ones available in repositories?

    1. Can i just "copy-paste" all dependent `.so` files into target OS? What should i consider when "copy-pasting" libraries from my development workstation to user's one?

Top answer
1 of 2
4
What the difference between libc++ and libstdc++? libc++ is part of clang project and libstdc++ is part of GCC project. They are compatible for C++17 and older versions, but C++20 implementation is not ready, so they have differences in implemented features, we'll have to wait until they are done. How to determine which versions of these libs support specific standard version? For example which version of c++ standard does libstdc++-4.6 support? In their documentation, for example you can check what version GCC libstdc++ supports here: https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html Copy pasting libs may or may not work, depending on many things. It's best to just install standard library using package manager of your operating system.
2 of 2
1
(mine) 0. What are libc++ and libstdc++ They are compiler-specific implementations of the C++ standard library. libc++ is bundled with Clang and libstdc++ with GCC. On many unix platforms Clang works with libstdc++ by default for better ABI compatibility. 1. What the difference between libc++ and libstdc++? Why can't we just use one of them for all times? They are different implementations of the same library. Your code should not really care which one it is using, unless you also want to use compiler-specific extensions. 2. How to determine which versions of these libs support specific standard version? Not sure. Look into GCC and Clang documentation. 3. Can i just "copy-paste" these libs into target OS to run my project if that OS lacks newer versions of these ones available in repositories? Can i just "copy-paste" all dependent .so files into target OS? What should i consider when "copy-pasting" libraries from my development workstation to user's one? Generally, yes but you need to take care of few things: Your executable must be able to locate the library object. This might require to edit LD_LIBRARY_PATH or inject rpath information into the executable. Licensing concerns (???) - IIRC even majority of commercial uses are fine though It's possible to use static version of the standard library - then it's inside the executable. Whatever linking you are using, the target OS must support all dependencies of the standard library implementation. This will usually be dependent on supported syscalls which are the border between user-space and kerner-space.