The LIBRARY_PATH environment variable is pretty standard. It is known to majority of compilers.

You should also use C_INCLUDE_PATH and/or CPLUS_INCLUDE_PATH. These two a more gcc specific (other compilers prefer INCLUDE without language separation).

You can also ignore the environment variables completely and specify the correct libstdc++ directly in the command line.

g++ main.cpp /software/gcc10/.../libstdc++.a
Answer from White Owl on Stack Exchange
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › manual › abi.html
ABI Policy and Guidelines - GCC, the GNU Compiler Collection
Later versions defined it in "c-common.c" in the gcc directory, and from G++ 3.4 it is defined in c-cppbuiltin.c and its value determined by the '-fabi-version' command-line option. It is versioned as follows, where 'n' is given by '-fabi-version=n': ... Changes to the default compiler option for -fabi-version. ... Incremental bumping of a library pre-defined macro. For releases before 3.4.0, the macro is __GLIBCPP__. For later releases, it's __GLIBCXX__. (The libstdc++ project generously changed from CPP to CXX throughout its source to allow the "C" pre-processor the CPP macro namespace.)
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++
The GNU C++ Library
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 Texts, and with no Back-Cover Texts.
Discussions

c++ - How do you find what version of libstdc++ library is installed on your linux machine? - Stack Overflow
The table of datestamps of libstdc++ versions is listed in the documentation: ... Sign up to request clarification or add additional context in comments. ... The datestamps are almost entirely useless, I don't know why we bother keeping them or documenting them. For example, the date for GCC 4.6.3 ... More on stackoverflow.com
🌐 stackoverflow.com
How can I know the libstdc++ version shipped with each gcc version? - Stack Overflow
How can I know the libstdc++ version shipped with each gcc version? Is there an an easy way to get this info without the need to install the gcc? More on stackoverflow.com
🌐 stackoverflow.com
libstdc++ vs libstdc++11 when using gcc version 8+ and -std=c++17

What is libstdc++11?

More on reddit.com
🌐 r/cpp
21
14
July 14, 2020
gcc - What is libstdc++.so.6 and GLIBCXX_3.4.20? - Unix & Linux Stack Exchange
[affans@hpc lib64]$ cd /usr/local/lib64 ... -rw-r--r-- 1 root root 2313 May 16 2019 libstdc++.so.6.0.20-gdb.py · 1) Question is why do I have different versions of these libraries in different folders? and 2) final question, when running gcc how do I tell it to use the 6.0.20 library ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › faq.html
Frequently Asked Questions
4.1. Can libstdc++ be used with non-GNU compilers? 4.2. No 'long long' type on Solaris? 4.3. _XOPEN_SOURCE and _GNU_SOURCE are always defined? 4.4. Mac OS X ctype.h is broken! How can I fix it? 4.5. Threading is broken on i386? 4.6. MIPS atomic operations · 4.7. Recent GNU/Linux glibc required? 4.8. Can't use wchar_t/wstring on FreeBSD · 5.1. What works already? 5.2. Bugs in the ISO C++ language or library specification · 5.3. Bugs in the compiler (gcc/g++) and not libstdc++ 6.1.
Top answer
1 of 4
124

To find which library is being used you could run

Copy $ /sbin/ldconfig -p | grep stdc++
    libstdc++.so.6 (libc6) => /usr/lib/libstdc++.so.6

The list of compatible versions for libstdc++ version 3.4.0 and above is provided by

Copy $ strings /usr/lib/libstdc++.so.6 | grep LIBCXX
 GLIBCXX_3.4
 GLIBCXX_3.4.1
 GLIBCXX_3.4.2
 ...

For earlier versions the symbol GLIBCPP is defined.

The date stamp of the library is defined in a macro __GLIBCXX__ or __GLIBCPP__ depending on the version:

Copy// libdatestamp.cxx
#include <cstdio>

int main(int argc, char* argv[]){
#ifdef __GLIBCPP__
    std::printf("GLIBCPP: %d\n",__GLIBCPP__);
#endif
#ifdef __GLIBCXX__
    std::printf("GLIBCXX: %d\n",__GLIBCXX__);
#endif
   return 0;
}

$ g++ libdatestamp.cxx -o libdatestamp
$ ./libdatestamp
GLIBCXX: 20101208

The table of datestamps of libstdc++ versions is listed in the documentation:

2 of 4
22

What exactly do you want to know?

The shared library soname? That's part of the filename, libstdc++.so.6, or shown by readelf -d /usr/lib64/libstdc++.so.6 | grep soname.

The minor revision number? You should be able to get that by simply checking what the symlink points to:

Copy$ ls -l  /usr/lib/libstdc++.so.6
lrwxrwxrwx. 1 root root 19 Mar 23 09:43 /usr/lib/libstdc++.so.6 -> libstdc++.so.6.0.16

That tells you it's 6.0.16, which is the 16th revision of the libstdc++.so.6 version, which corresponds to the GLIBCXX_3.4.16 symbol versions.

Or do you mean the release it comes from? It's part of GCC so it's the same version as GCC, so unless you've screwed up your system by installing unmatched versions of g++ and libstdc++.so you can get that from:

Copy$ g++ -dumpversion
4.6.3

Or, on most distros, you can just ask the package manager. On my Fedora host that's

Copy$ rpm -q libstdc++
libstdc++-4.6.3-2.fc16.x86_64
libstdc++-4.6.3-2.fc16.i686

As other answers have said, you can map releases to library versions by checking the ABI docs

🌐
Pale Moon Forum
forum.palemoon.org › viewtopic.php
Linux users: What version of glibc and libstdc++ is everyone on? - Pale Moon forum
December 23, 2023 - core/glibc 2.38-7 [installed] GNU ...linux-gnu-gcc 13.2.0-1 [installed] The GNU Compiler Collection - cross compiler for ARM64 target · Pale Moon and Epyrus on Arch Linux. ... Using andyprough's commands. ldd (Ubuntu GLIBC 2.35-0ubuntu3.5) 2.35 libstdc++6: Installed: 12.3.0-1ubuntu1~22.04 Merry Christmas! ... For Linux machines I am on various Debian versions, with newer ...
🌐
Reddit
reddit.com › r/cpp › libstdc++ vs libstdc++11 when using gcc version 8+ and -std=c++17
r/cpp on Reddit: libstdc++ vs libstdc++11 when using gcc version 8+ and -std=c++17
July 14, 2020 -

Is there a difference between libstdc++ and libstdc++11 in the current releases of gcc (or at least gcc >=8)?

I want to create libraries that support code using the C++17 standard.

I can't find any concise answer on which lib to use or if it even matters.

Find elsewhere
🌐
GitHub
gist.github.com › ernstki › 593076d2d164ca51d4a6c0a15731846f
List the ABI versions of all detected libc and libstdc++'s (GNU/Linux only) · GitHub
The libcversions.sh included as a part of this gist reports which libc and libstdc++ ABI versions are available to running programs. It does this by inspecting the output of ldconfig as well as the value of LD_LIBRARY_PATH, which is usually modified by modulefiles (e.g., when you module load gcc/4.8.5).
🌐
Debian
packages.debian.org › sid › libstdc++6
Debian -- Details of package libstdc++6 in sid
The first version of libstdc++-v3 appeared in g++-3.0. Tags: Role: Shared Library · dep: gcc-14-base (= 14-20240221-2.1) [ia64] GCC, the GNU Compiler Collection (base package) dep: gcc-16-base (= 16.1.0-1) [not ia64] GCC, the GNU Compiler Collection (base package) dep: libc6 (>= 2.38) [not alpha, i386, ia64, loong64, sh4] GNU C Library: Shared libraries also a virtual package provided by libc6-udeb ·
🌐
MIT
web.mit.edu › darwin › src › modules › gcc3 › libstdc++-v3 › docs › html › faq
libstdc++-v3 FAQ
- more named locale bug fixes - support for symbol versioning when using GNU ld >= 2.12 - wide-io - tuning for executable size · This is by no means meant to be complete nor exhaustive, but mentions some problems that users may encounter when building or using libstdc++. If you are experiencing one of these problems, you can find more information on the libstdc++ and the GCC ...
🌐
Linux From Scratch
linuxfromscratch.org › lfs › view › 12.3-systemd › chapter05 › gcc-libstdc++.html
5.6. Libstdc++ from GCC-14.2.0
You should first unpack the GCC tarball and change to the gcc-14.2.0 directory. Create a separate build directory for Libstdc++ and enter it:
🌐
MIT
web.mit.edu › darwin › src › modules › gcc3 › libstdc++-v3 › docs › html › install.html
libstdc++-v3 Installation Instructions
If you are using a 3.1-series libstdc++ snapshot, then the requirements are slightly more stringent: the compiler sources must also be 3.1 or later (for both technical and licensing reasons), and your binutils must be 2.11.95 or later if you want to use symbol versioning in shared libraries. The following definitions will be used throughout the rest of this document: gccsrcdir: The directory holding the source of the compiler.
🌐
CERN
root-forum.cern.ch › cling
Cling: How to work with two versions of libstdc++.so? - Cling - ROOT Forum
March 1, 2017 - In a command line I could do (i.e. compile a program with gcc-4.9.3 and link against a library which requires a newer version of libstdc++.so than the one supplied with gcc-4.9.3): $ g++ -v ... gcc version 4.9.3 (GCC) $ g++ ... -L$HOME/bin/gcc-5.4.0/lib64 \ -Wl,-rpath=$HOME/bin/gcc-5.4.0/lib64 ...
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.

🌐
Medium
medium.com › @fixitblog › solved-setup-libstdc-for-a-given-gcc-version-25d0123be36f
Setup libstdc++ for a given gcc version | by Ted James | Medium
November 1, 2024 - sudo update-alternatives --install /usr/bin/gcc gcc /usr/local/gcc-14.1.0/bin/gcc-14.1.0 14 --slave /usr/bin/g++ g++ /usr/local/gcc-14.1.0/bin/g++-14.1.0 --slave /usr/bin/gcov gcov /usr/local/gcc-14.1.0/bin/gcov-14.1.0 · However when compiling cpp code that use c++23 features I get link error: /lib/x86_64-linux-gnu/libstdc++.so.6: version `GLIBCXX_3.4.31' not found
🌐
Debian
packages.debian.org › sid › libstdc++-13-dev
Debian -- Details of package libstdc++-13-dev in sid
The first version of libstdc++-v3 appeared in g++-3.0. dep: gcc-13-base (= 13.2.0-23) [ia64] GCC, the GNU Compiler Collection (base package) dep: gcc-13-base (= 13.4.0-10) [not alpha, ia64, loong64, sh4] dep: gcc-13-base (= 13.4.0-4) [loong64] dep: gcc-13-base (= 13.4.0-5) [alpha] dep: gcc-13-base (= 13.4.0-9) [sh4] dep: libc6 (>= 2.38) [not alpha, i386, ia64, loong64, sh4] GNU C Library: Shared libraries also a virtual package provided by libc6-udeb ·
🌐
Linux From Scratch
linuxfromscratch.org › lfs › view › development › chapter05 › gcc-libstdc++.html
5.6. Libstdc++ from GCC-16.1.0
You should first unpack the GCC tarball and change to the gcc-16.1.0 directory. Create a separate build directory for Libstdc++ and enter it: