g++: How to link against custom libstdc++ version?
Fredrik Orderud
forderud@gmail.com
Thu Nov 20 11:21:48 GMT 2025
I'm working on a project developing a Linux shared library (.so) with
a pure "C" interface that we want to be as widely compatible as
possible with multiple distros of various ages. We've previously
attempted to use -static-libstdc++ to encapsulate libstdc++ usage, but
that led to problems previously discussed in this channel in November
2025. We're therefore instead considering bundling libstdc++.so.6 with
our shared library in an attempt to allow callers with older
GCC/libstdc++ versions to still be able to use the library.
The approach of bundling libstdc++.so.6 with our shared lib. seem to
work mostly fine if the caller is combining it with either
-Wl,-rpath=$ORIGIN or LD_LIBRARY_PATH=`pwd` to ensure that the newer
bundled libstdc++ version is used instead of the older version
included in their old GCC installation.
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. 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_.
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++. 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?
Thanks in advance,
Fredrik
More information about the Libstdc++
mailing list