g++: How to link against custom libstdc++ version?
Fredrik Orderud
forderud@gmail.com
Thu Nov 20 12:06:08 GMT 2025
On Thu, Nov 20, 2025 at 12:36 PM Jonathan Wakely <jwakely.gcc@gmail.com> wrote:
> On Thu, 20 Nov 2025 at 11:22, Fredrik Orderud <forderud@gmail.com> wrote:
> > One problem though is the g++ seem to always want to implicitly link
> > to the libstdc++ version included with the GCC installation. We
> > instead want g++ to link to the newer libstdc++.so.6 found in the
> > current folder.
>
> Isn't it sufficient to use the newer libstdc++.so.6 at runtime? Why do
> you need to use it at link-time?
Our customers need to link our shared lib (that's built against
libstdc++ 13) into their application with an old g++ 7. This triggers
the following linker errors unless passing -nodefaultlibs :
./libModernStuff.so: undefined reference to
`std::__exception_ptr::exception_ptr::_M_release()@CXXABI_1.3.13'
./libModernStuff.so: undefined reference to
`std::__exception_ptr::exception_ptr::_M_addref()@CXXABI_1.3.13'
./libModernStuff.so: undefined reference to
`std::__throw_bad_array_new_length()@GLIBCXX_3.4.29'
collect2: error: ld returned 1 exit status
> > Passing -L. doesn't help address this, since it only
> > seems to add the current folder to the _end_ of the library search
> > list. We instead want to instruct g++ to search for libraries in the
> > current folder _first_.
>
> Right.
>
> > We've already managed to work around this problem by either replacing
> > g++ with gcc, or by passing -nodefaultlibs to prevent g++/gcc from
> > linking implicitly to libstdc++.
>
> You probably want -nostdlib++ for this.
Thanks for the tip, Jonathan! We're unfortunately unable to adopt this
flag yet, since it was was only introduced in GCC 13. I'll take note
and ensure that we'll adopt it when dropping support for pre-GCC 13
clients in the future.
> > The new libstdc++ library is still
> > linked in transitively though our shared library though, so all is
> > well. However, it's rather awkward for callers to not be able to
> > combine our shared library with regular g++ commands. I'm therefore
> > seeking advise on the topic.
> >
> > Are there any mechanisms for instructing g++ to link against a
> > user-provided libstdc++.so.6 instead of the one included in the GCC
> > installation?
>
> It should work to use -nostdlib++ and add the full path to the newer
> libstdc++.so.6
Thanks again for quick, actionable advice, Jonathan! I've just
verified that we can combine -nodefaultlibs with explicit linking to
the full path to the newer libstdc++.so.6. We'll replace this with
-nostdlib++ in the future. We'll still need to document that customers
need to call g++ with -nodefaultlibs or -nostdlib++, but I guess we
can live with that.
Best regards,
Fredrik
More information about the Libstdc++
mailing list