Safe to link to static libstdc++ from shared library?
Jonathan Wakely
jwakely@redhat.com
Fri Nov 14 15:50:55 GMT 2025
On Fri, 14 Nov 2025 at 15:31, Fredrik Orderud <forderud@gmail.com> wrote:
>
> > > We're already building our shared library in an old Linux container
> > > with glibc 2.27 and libstdc++ 20191114 for improved backwards
> >
> > I don't know what libstdc++ 20191114 is. Presumably the value of
> > __GLIBCXX__ but that's not meaningful, see the discussion of
> > __GLIBCXX__ at https://gcc.gnu.org/onlinedocs/libstdc++/manual/abi.html#abi.versioning.history
> > (and the suggestion to use _GLIBCXX_RELEASE instead).
>
> Thanks' for the suggestion! Just checked, and we're using libstdc++ 7
> in the docker build image.
>
> > > compatibility. However, it then becomes a tension between the oldest
> > > libstdc++ version supported and the newest C++ version supported. We
> > > basically want to adopt C++23 and newer internally in our shared lib
> > > while still being compatible with old systems. My hope was that static
> > > linking to libstdc++ could help eliminate that problem.
> >
> > That's unlikely to work, because the C++23 ABI is not stable yet. You
> > can't mix and match any objects compiled with C++23 mode by different
> > GCC versions, because the C++23 features like std::expected might not
> > be ABI compatible between GCC 13 and GCC 14, for instance.
>
> I might be colored by my Windows background here, but I though that
> C++ ABI compatibility isn't a problem as long as our shared library
> only uses C++ _internally_, while the API i in pure "C" with no
> exceptions escaping. However, I might have been naive here(?)
Linux works differently. ELF has a single address space for all
symbols. You can play games with RTLD_LOCAL and symbol visibility, but
it's not reliable for C++ because there are C++ runtime symbols which
really do require that they are globally unique in the program image.
Which from a quick glance is the reason for your segfaults.
More information about the Libstdc++
mailing list