configure: add --with-cxx-stdlib option

When C++ support is enabled, configure unconditionally probes
`-lstdc++` and keeps it in LIBS if the link test succeeds. On
platforms using libc++, this probe can succeed at compile time (if
libstdc++ headers/libraries happen to be installed) but then cause
runtime failures when configure tries to execute test binaries
because `libstdc++.so.6` isn't actually available.

Add a `--with-cxx-stdlib=<libstdc++|libcxx>` option that lets the
build system specify which C++ standard library to link. When given,
the probe is skipped and the specified library is linked directly.
When not given, the original probe behavior is preserved.
This commit is contained in:
Yuxuan Chen 2026-02-24 18:12:56 -08:00
parent 1972241cd2
commit 0da809bf4f

View file

@ -324,6 +324,15 @@ fi
,
enable_cxx="1"
)
AC_ARG_WITH([cxx_stdlib],
[AS_HELP_STRING([--with-cxx-stdlib=<libstdc++|libcxx>],
[Specify the C++ standard library to link (default: probe for libstdc++)])],
[case "${with_cxx_stdlib}" in
libstdc++|libcxx) ;;
*) AC_MSG_ERROR([bad value ${with_cxx_stdlib} for --with-cxx-stdlib]) ;;
esac],
[with_cxx_stdlib=""]
)
if test "x$enable_cxx" = "x1" ; then
dnl Require at least c++14, which is the first version to support sized
dnl deallocation. C++ support is not compiled otherwise.
@ -338,17 +347,28 @@ if test "x$enable_cxx" = "x1" ; then
JE_CXXFLAGS_ADD([-g3])
SAVED_LIBS="${LIBS}"
JE_APPEND_VS(LIBS, -lstdc++)
JE_COMPILABLE([libstdc++ linkage], [
case "${with_cxx_stdlib}" in
libstdc++)
JE_APPEND_VS(LIBS, -lstdc++)
;;
libcxx)
JE_APPEND_VS(LIBS, -lc++)
;;
*)
dnl Probe for libstdc++ (the default when --with-cxx-stdlib is not given).
JE_APPEND_VS(LIBS, -lstdc++)
JE_COMPILABLE([libstdc++ linkage], [
#include <stdlib.h>
], [[
int *arr = (int *)malloc(sizeof(int) * 42);
if (arr == NULL)
return 1;
]], [je_cv_libstdcxx])
if test "x${je_cv_libstdcxx}" = "xno" ; then
LIBS="${SAVED_LIBS}"
fi
if test "x${je_cv_libstdcxx}" = "xno" ; then
LIBS="${SAVED_LIBS}"
fi
;;
esac
else
enable_cxx="0"
fi