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++
The GNU C++ Library
This is the top level of the libstdc++ documentation set.
🌐
Linux From Scratch
linuxfromscratch.org › lfs › view › development › chapter05 › gcc-libstdc++.html
5.6. Libstdc++ from GCC-16.1.0
Libstdc++ is the standard C++ library. It is needed to compile C++ code (part of GCC is written in C++), but we had to defer its installation when we built gcc-pass1 because Libstdc++ depends on Glibc, which was not yet available in the target directory.
Discussions

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
c++ - Setup libstdc++ for a given gcc version - Stack Overflow
New to Linux here. Already had gcc 11 and 12 on my ubuntu but compiled gcc-14 using following command: ./configure -v --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --pr... More on stackoverflow.com
🌐 stackoverflow.com
How to edit and re-build the GCC libstdc++ C++ standard library source? - Stack Overflow
I am working on some research and would like to edit some of the source code in the libstdc++ library for experimentation. I am, specifically, interested in experimenting with the parallel sorting More on stackoverflow.com
🌐 stackoverflow.com
c++ - When is it necessary to use the flag -stdlib=libstdc++? - Stack Overflow
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.1... More on stackoverflow.com
🌐 stackoverflow.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...
🌐
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.

🌐
GitHub
github.com › gcc-mirror › gcc › blob › master › libstdc++-v3 › include › precompiled › stdc++.h
gcc/libstdc++-v3/include/precompiled/stdc++.h at master · gcc-mirror/gcc
// permissions described in the GCC Runtime Library Exception, version · // 3.1, as published by the Free Software Foundation. · // You should have received a copy of the GNU General Public License and · // a copy of the GCC Runtime Library Exception along with this program; // see the files COPYING3 and COPYING.RUNTIME respectively.
Author   gcc-mirror
🌐
MIT
web.mit.edu › darwin › src › modules › gcc3 › libstdc++-v3 › docs › html › install.html
libstdc++-v3 Installation Instructions
Check out or download the GCC sources: the resulting source directory (gcc or gcc-3.0.3, for example) is gccsrcdir. Once in gccsrcdir, you'll need to rename or delete the libstdc++-v3 directory which comes with that snapshot:
Find elsewhere
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › manual › configure.html
Configure - GCC, the GNU Compiler Collection
Consider using the toplevel gcc configuration option --enable-languages=c++, which saves time by only building the C++ toolchain. Here are all of the configure options specific to libstdc++. Keep in mind that they all have opposite forms as well (enable/disable and with/without).
🌐
MIT
web.mit.edu › darwin › src › modules › gcc3 › libstdc++-v3 › docs › html › faq
libstdc++-v3 FAQ
For the bold and/or desperate, the GCC FAQ describes where to find the last libg++ source. If you have read the README and RELEASE-NOTES files, and your question remains unanswered, then just ask the mailing list. At present, you do not need to be subscribed to the list to send a message to it. More information is available on the homepage (including how to browse the list archives); to send to the list, use libstdc++@gcc.gnu.org.
🌐
Red Hat
developers.redhat.com › articles › 2023 › 04 › 03 › leaner-libstdc-gcc-13
A leaner in libstdc++ for GCC 13 | Red Hat Developer
August 14, 2023 - Learn about an upcoming change in GCC 13's libstdc++ that reduces executable size and improves startup times for C++ programs that use the header.
Top answer
1 of 2
12

Yes, you have to build the whole of GCC, but once you've done that you only need to rebuild the libstdc++ part.

Building GCC is described at http://gcc.gnu.org/wiki/InstallingGCC

The libstdc++ sources are in the libstdc++-v3 directory. The parallel algorithms are in libstdc++-v3/include/parallel, they are templates so all the code is in headers. The small amount of non-header code is in libstdc++-v3/src/c++98/parallel-settings.cc

After configuring and building the whole of GCC as normal, you can rebuild libstdc++ by running make in the $TARGET/libstdc++-v3 directory (where $TARGET is something like x86_64-pc-linux-gnu).

By default the makefiles don't have proper dependencies that cause objects to be rebuilt after headers change, so you might need to do make clean then make again to get your changes to be picked up.

2 of 2
11

Minimal step-by-step example

Compile GCC from source. Condensed commands:

sudo apt-get build-dep gcc
git clone git://gcc.gnu.org/git/gcc.git
cd gcc
git checkout gcc-6_4_0-release
./contrib/download_prerequisites
mkdir build
cd build
../configure --enable-languages=c,c++ --prefix="$(pwd)/install"
make -j`nproc`
make install

Wait from 30-minutes to two hours. Now let's use this test program a.cpp:

#include <cassert>
#include <queue>

int main() {
    std::priority_queue<int> q;
    q.emplace(2);
    q.emplace(1);
    q.emplace(3);
    assert(q.top() == 3);
    q.pop();
    assert(q.top() == 2);
    q.pop();
    assert(q.top() == 1);
    q.pop();
}

First compile and run it to ensure that the initial compilation worked:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

Now let's hack up the priority_queue constructor.

First, you can find the actual constructor easily with GDB as explained at: When should I use make_heap vs. Priority Queue?

So we hack it up with this patch:

diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h
index 5d255e7300b..deec7bc4d99 100644
--- a/libstdc++-v3/include/bits/stl_queue.h
+++ b/libstdc++-v3/include/bits/stl_queue.h
@@ -61,6 +61,7 @@
 #if __cplusplus >= 201103L
 # include <bits/uses_allocator.h>
 #endif
+#include <iostream>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -444,7 +445,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       priority_queue(const _Compare& __x = _Compare(),
             _Sequence&& __s = _Sequence())
       : c(std::move(__s)), comp(__x)
-      { std::make_heap(c.begin(), c.end(), comp); }
+      {
+        std::cout << "hacked" << std::endl;
+        std::make_heap(c.begin(), c.end(), comp);
+      }
 
       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
    explicit

Then rebuild and re-install just libstdc++ to save a lot of time:

cd gcc/build/x86_64-pc-linux-gnu/libstdc++-v3
make -j`nproc`
make install

and now the next build and run:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

outputs:

hacked

Tested on Ubuntu 16.04.

Ubuntu 22.04, GCC 12.1

We need to either:

  • add --disable-multilib to ./configure
  • (untested) sudo apt install gcc-multilib g++-multilib, these are linked to support for 32-bit executables: How to Compile 32-bit Apps on 64-bit Ubuntu?

or else it fails with:

configure: WARNING: using in-tree isl, disabling version check          
*** This configuration is not supported in the following subdirectories:                                                                                                                                           
     gnattools gotools target-libada target-libphobos target-zlib target-libbacktrace target-libgfortran target-libgo target-libffi target-libobjc target-liboffloadmic
    (Any other directories should still work fine.) 
checking for default BUILD_CONFIG... bootstrap-debug
checking for --enable-vtable-verify... no
/usr/bin/ld: cannot find Scrt1.o: No such file or directory
/usr/bin/ld: cannot find crti.o: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/11/libgcc.a when searching for -lgcc
/usr/bin/ld: cannot find -lgcc: No such file or directory
collect2: error: ld returned 1 exit status                                                               
configure: error: I suspect your system does not have 32-bit development libraries (libc and headers). If you have them, rerun configure with --enable-multilib. If you do not have them, and want to build a 64-bi
t-only compiler, rerun configure with --disable-multilib. 

glibc

As a bonus, if you are also interested in C: Multiple glibc libraries on a single host

Related:

  • https://unix.stackexchange.com/questions/657370/how-to-compile-libstdc-with-specific-compiler-option
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 › gcc-mirror › gcc › tree › master › libstdc++-v3
gcc/libstdc++-v3 at master · gcc-mirror/gcc
April 2, 2020 - file: libstdc++-v3/README New users may wish to point their web browsers to the file index.html in the 'doc/html' subdirectory.
Author   gcc-mirror
🌐
Linux From Scratch
linuxfromscratch.org › lfs › view › 9.1-systemd › chapter05 › gcc-libstdc++.html
5.8. Libstdc++ from GCC-9.2.0
Libstdc++ is the standard C++ library. It is needed to compile C++ code (part of GCC is written in C++), but we had to defer its installation when we built gcc-pass1 because it depends on glibc, which was not yet available in /tools.
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › manual › setup.html
Chapter 2. Setup
If GCC 3.1.0 or later on is being used on GNU/Linux, an attempt will be made to use "C" library functionality necessary for C++ named locale support, e.g. the newlocale and uselocale functions. For GCC 4.6.0 and later, this means that glibc 2.3 or later is required. If the 'gnu' locale model is being used, the following locales are used and tested in the libstdc++ testsuites.
🌐
GNU
gcc.gnu.org › onlinedocs › libstdc++ › latest-doxygen › index.html
libstdc++ Source Documentation
August 23, 2024 - There are two types of documentation for libstdc++. One is the distribution documentation, which can be read online here or offline from the file doc/html/index.html in the library source directory.
Top answer
1 of 2
4

Hints https://www.linuxfromscratch.org/lfs/view/stable/chapter06/gcc-pass2.html

A test-build ....

tar xvf gcc-10.3.0.tar.xz
mkdir BUILD__libstdc++103
cd BUILD__libstdc++103/

../gcc-10.3.0/libstdc++-v3/configure \
    CXXFLAGS="-g -O2 -D_GNU_SOURCE -fno-omit-frame-pointer" \
    --prefix=/home/knudfl/BUILD__libstdc++103/usr \
    --disable-multilib --disable-libstdcxx-pch 

make
make install

Seems to be OK : The text -fno-omit-frame-pointer is visible in the terminal make output.

2 of 2
0

Minimal step-by-step example

Compile GCC from source. Condensed commands:

sudo apt-get build-dep gcc
git clone git://gcc.gnu.org/git/gcc.git
cd gcc
git checkout gcc-6_4_0-release
./contrib/download_prerequisites
mkdir build
cd build
../configure --enable-languages=c,c++ --prefix="$(pwd)/install"
make -j`nproc`

Wait from 30-minutes to two hours. Now let's use this test program a.cpp:

#include <cassert>
#include <queue>

int main() {
    std::priority_queue<int> q;
    q.emplace(2);
    q.emplace(1);
    q.emplace(3);
    assert(q.top() == 3);
    q.pop();
    assert(q.top() == 2);
    q.pop();
    assert(q.top() == 1);
    q.pop();
}

First compile and run it to ensure that the initial compilation worked:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

Now let's hack up the priority_queue constructor.

First, you can find the actual constructor easily with GDB as explained at: https://stackoverflow.com/questions/11266360/when-should-i-use-make-heap-vs-priority-queue/51945521#51945521

So we hack it up with this patch:

diff --git a/libstdc++-v3/include/bits/stl_queue.h b/libstdc++-v3/include/bits/stl_queue.h
index 5d255e7300b..deec7bc4d99 100644
--- a/libstdc++-v3/include/bits/stl_queue.h
+++ b/libstdc++-v3/include/bits/stl_queue.h
@@ -61,6 +61,7 @@
 #if __cplusplus >= 201103L
 # include <bits/uses_allocator.h>
 #endif
+#include <iostream>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -444,7 +445,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       priority_queue(const _Compare& __x = _Compare(),
             _Sequence&& __s = _Sequence())
       : c(std::move(__s)), comp(__x)
-      { std::make_heap(c.begin(), c.end(), comp); }
+      {
+        std::cout << "hacked" << std::endl;
+        std::make_heap(c.begin(), c.end(), comp);
+      }
 
       template<typename _Alloc, typename _Requires = _Uses<_Alloc>>
    explicit

Then rebuild and re-install just libstdc++ to save a lot of time:

cd gcc/build/x86_64-pc-linux-gnu/libstdc++-v3
make -j`nproc`
make install

and now the next build and run:

gcc/build/install/bin/g++ -g -std=c++11 -O0 -o a.out ./a.cpp
./a.out

outputs:

hacked

Tested on Ubuntu 16.04.

glibc

As a bonus, if you are also interested in C: https://stackoverflow.com/questions/847179/multiple-glibc-libraries-on-a-single-host/52454603#52454603

Related:

  • https://stackoverflow.com/questions/21872229/how-to-edit-and-re-build-the-gcc-libstdc-c-standard-library-source/51946224#51946224
🌐
Linux From Scratch
linuxfromscratch.org › lfs › view › 12.3-systemd › chapter05 › gcc-libstdc++.html
5.6. Libstdc++ from GCC-14.2.0
Libstdc++ is the standard C++ library. It is needed to compile C++ code (part of GCC is written in C++), but we had to defer its installation when we built gcc-pass1 because Libstdc++ depends on Glibc, which was not yet available in the target directory.