Detect if flags are needed for C++11 support
authorPeter Eisentraut <peter@eisentraut.org>
Thu, 22 Jan 2026 07:39:39 +0000 (08:39 +0100)
committerPeter Eisentraut <peter@eisentraut.org>
Thu, 22 Jan 2026 08:09:25 +0000 (09:09 +0100)
Just like we only support compiling with C11, we only support
compiling extensions with C++11 and up.  Some compilers support C++11
but don't enable it by default.  This detects if flags are needed to
enable C++11 support, in a similar way to how we check the same for
C11 support.

The C++ test extension module added by commit 476b35d4e31 confirmed
that C++11 is effectively required.  (This was understood in mailing
list discussions but not recorded anywhere in the source code.)

Author: Jelte Fennema-Nio <postgres@jeltef.nl>
Co-authored-by: Peter Eisentraut <peter@eisentraut.org>
Discussion: https://www.postgresql.org/message-id/flat/E1viDt1-001d7E-2I%40gemulon.postgresql.org

configure
configure.ac
meson.build

index 04eeb1a741c0bdf403aeb8efcb1f621bcc08c431..a10a2c85c6ab43c2e6b1ff3500666388784e04fc 100755 (executable)
--- a/configure
+++ b/configure
@@ -4770,11 +4770,95 @@ ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $
 ac_compiler_gnu=$ac_cv_c_compiler_gnu
 
 
-if test -n "$ac_ct_CXX"; then
+# Check if it actually found a C++ compiler.
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+
+cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+
+int
+main ()
+{
+
+  ;
+  return 0;
+}
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
   have_cxx=yes
 else
   have_cxx=no
 fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+
+
+if test "$have_cxx" = yes; then
+
+# Detect option needed for C++11
+{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for $CXX option to accept ISO C++11" >&5
+$as_echo_n "checking for $CXX option to accept ISO C++11... " >&6; }
+if ${pgac_cv_prog_cxx_cxx11+:} false; then :
+  $as_echo_n "(cached) " >&6
+else
+  pgac_cv_prog_cxx_cxx11=no
+pgac_save_CXX=$CXX
+ac_ext=cpp
+ac_cpp='$CXXCPP $CPPFLAGS'
+ac_compile='$CXX -c $CXXFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CXX -o conftest$ac_exeext $CXXFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_cxx_compiler_gnu
+
+for pgac_arg in '' '-std=gnu++11' '-std=c++11'; do
+  CXX="$pgac_save_CXX $pgac_arg"
+  cat confdefs.h - <<_ACEOF >conftest.$ac_ext
+/* end confdefs.h.  */
+#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif
+_ACEOF
+if ac_fn_cxx_try_compile "$LINENO"; then :
+  pgac_cv_prog_cxx_cxx11=$pgac_arg
+fi
+rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
+  test x"$pgac_cv_prog_cxx_cxx11" != x"no" && break
+done
+ac_ext=c
+ac_cpp='$CPP $CPPFLAGS'
+ac_compile='$CC -c $CFLAGS $CPPFLAGS conftest.$ac_ext >&5'
+ac_link='$CC -o conftest$ac_exeext $CFLAGS $CPPFLAGS $LDFLAGS conftest.$ac_ext $LIBS >&5'
+ac_compiler_gnu=$ac_cv_c_compiler_gnu
+
+CXX=$pgac_save_CXX
+fi
+
+
+if test x"$pgac_cv_prog_cxx_cxx11" = x"no"; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: unsupported" >&5
+$as_echo "unsupported" >&6; }
+  { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: C++ compiler \"$CXX\" does not support C++11" >&5
+$as_echo "$as_me: WARNING: C++ compiler \"$CXX\" does not support C++11" >&2;}
+  have_cxx=no
+elif test x"$pgac_cv_prog_cxx_cxx11" = x""; then
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: none needed" >&5
+$as_echo "none needed" >&6; }
+else
+  { $as_echo "$as_me:${as_lineno-$LINENO}: result: $pgac_cv_prog_cxx_cxx11" >&5
+$as_echo "$pgac_cv_prog_cxx_cxx11" >&6; }
+  CXX="$CXX $pgac_cv_prog_cxx_cxx11"
+fi
+
+fi  # have_cxx
 
 
 # Check if it's Intel's compiler, which (usually) pretends to be gcc,
@@ -14764,7 +14848,6 @@ cat >>confdefs.h <<_ACEOF
 _ACEOF
 
 
-
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking for C++ printf format archetype" >&5
 $as_echo_n "checking for C++ printf format archetype... " >&6; }
 if ${pgac_cv_cxx_printf_archetype+:} false; then :
index 13c75170f7a6fdc91c0ab1521f030b211785cadc..814e64a967efa934d6bb9d72db7f7a5acf40c494 100644 (file)
@@ -387,12 +387,45 @@ fi
 
 AC_PROG_CXX([$pgac_cxx_list])
 
-if test -n "$ac_ct_CXX"; then
-  have_cxx=yes
-else
+# Check if it actually found a C++ compiler.
+AC_LANG_PUSH([C++])
+AC_COMPILE_IFELSE([AC_LANG_PROGRAM([], [])],
+  [have_cxx=yes],
+  [have_cxx=no])
+AC_LANG_POP([C++])
+AC_SUBST(have_cxx)
+
+if test "$have_cxx" = yes; then
+
+# Detect option needed for C++11
+AC_MSG_CHECKING([for $CXX option to accept ISO C++11])
+AC_CACHE_VAL([pgac_cv_prog_cxx_cxx11],
+[pgac_cv_prog_cxx_cxx11=no
+pgac_save_CXX=$CXX
+AC_LANG_PUSH([C++])
+for pgac_arg in '' '-std=gnu++11' '-std=c++11'; do
+  CXX="$pgac_save_CXX $pgac_arg"
+  AC_COMPILE_IFELSE([AC_LANG_SOURCE([[#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif]])], [[pgac_cv_prog_cxx_cxx11=$pgac_arg]])
+  test x"$pgac_cv_prog_cxx_cxx11" != x"no" && break
+done
+AC_LANG_POP([C++])
+CXX=$pgac_save_CXX])
+
+if test x"$pgac_cv_prog_cxx_cxx11" = x"no"; then
+  AC_MSG_RESULT([unsupported])
+  AC_MSG_WARN([C++ compiler "$CXX" does not support C++11])
   have_cxx=no
+elif test x"$pgac_cv_prog_cxx_cxx11" = x""; then
+  AC_MSG_RESULT([none needed])
+else
+  AC_MSG_RESULT([$pgac_cv_prog_cxx_cxx11])
+  CXX="$CXX $pgac_cv_prog_cxx_cxx11"
 fi
-AC_SUBST(have_cxx)
+
+fi  # have_cxx
+
 
 # Check if it's Intel's compiler, which (usually) pretends to be gcc,
 # but has idiosyncrasies of its own.  We assume icc will define
index 92850244ad9ddf967ff3f697da24bf3d22717802..df907b62da337d82566fd5852c9953c2edb499e8 100644 (file)
@@ -586,6 +586,33 @@ if not cc.compiles(c11_test, name: 'C11')
 endif
 
 
+# Do we need an option to enable C++11?
+cxx11_test = '''
+#if !defined __cplusplus || __cplusplus < 201103L
+# error "Compiler does not advertise C++11 conformance"
+#endif
+'''
+
+if have_cxx and not cxx.compiles(cxx11_test, name: 'C++11')
+  cxx11_ok = false
+  if cxx.get_id() == 'msvc'
+    cxx11_test_args = ['/std:c++14']  # oldest version supported
+  else
+    cxx11_test_args = ['-std=gnu++11', '-std=c++11']
+  endif
+  foreach arg : cxx11_test_args
+    if cxx.compiles(cxx11_test, name: 'C++11 with @0@'.format(arg), args: [arg])
+      cxx11_ok = true
+      cxxflags += arg
+      break
+    endif
+  endforeach
+  if not cxx11_ok
+    error('C++ compiler does not support C++11')
+  endif
+endif
+
+
 postgres_inc = [include_directories(postgres_inc_d)]
 test_lib_d = postgres_lib_d
 test_c_args = cppflags + cflags