diff --git a/libgomp/Makefile.in b/libgomp/Makefile.in
index 2c81ccacc1dc940b034abac2e2e087151f493b7e..95585353b4e753ca6755f306df97e93ae822f53f 100644
--- a/libgomp/Makefile.in
+++ b/libgomp/Makefile.in
@@ -368,6 +368,7 @@ CFLAGS = @CFLAGS@
 CPP = @CPP@
 CPPFLAGS = @CPPFLAGS@
 CPU_COUNT = @CPU_COUNT@
+CXX = @CXX@
 CYGPATH_W = @CYGPATH_W@
 DEFS = @DEFS@
 DEPDIR = @DEPDIR@
diff --git a/libgomp/configure b/libgomp/configure
index fd0e337b5781749279a7a281c2eed87d7c3fd4e2..33f8471e6b444ce147b9a73481b6dd5589d3a89f 100755
--- a/libgomp/configure
+++ b/libgomp/configure
@@ -678,6 +678,7 @@ libtool_VERSION
 ac_ct_FC
 FCFLAGS
 FC
+CXX
 MAINT
 MAINTAINER_MODE_FALSE
 MAINTAINER_MODE_TRUE
@@ -11418,7 +11419,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11421 "configure"
+#line 11422 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11524,7 +11525,7 @@ else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 11527 "configure"
+#line 11528 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -11810,11 +11811,22 @@ fi
 
 
 
+# We optionally test libgomp C++ support, and for that want to use the proper
+# C++ driver, 'g++' (or 'xg++' for build-tree testing).  Given that build of
+# target libstdc++-v3 depends on target libgomp (see '../Makefile.def'), we
+# cannot make build of target libgomp depend on target libstdc++-v3: circular
+# dependency.  We thus cannot instantiate 'AC_PROG_CXX' here: we'd get
+# '-funconfigured-libstdc++-v3' (see '../configure.ac').  Therefore, just
+# capture 'CXX', and we'll fix this up at 'make check' time (see
+# 'testsuite/lib/libgomp.exp:libgomp_init').
+
+
 # Create a spec file, so that compile/link tests don't fail
 test -f libgfortran.spec || touch libgfortran.spec
 FCFLAGS="$FCFLAGS -L."
 
-# We need gfortran to compile parts of the library
+# We need 'gfortran' to compile parts of the library, and test libgomp Fortran
+# support.
 # We can't use AC_PROG_FC because it expects a fully working gfortran.
 #AC_PROG_FC(gfortran)
 case `echo $GFORTRAN` in
diff --git a/libgomp/configure.ac b/libgomp/configure.ac
index a9b1f3973f7b64b8a3e7ba2e2b4abe4f62f35bcf..46619d7127da29716380631aaa6a3cd92e89d2b0 100644
--- a/libgomp/configure.ac
+++ b/libgomp/configure.ac
@@ -152,11 +152,22 @@ AC_SUBST(enable_static)
 
 AM_MAINTAINER_MODE
 
+# We optionally test libgomp C++ support, and for that want to use the proper
+# C++ driver, 'g++' (or 'xg++' for build-tree testing).  Given that build of
+# target libstdc++-v3 depends on target libgomp (see '../Makefile.def'), we
+# cannot make build of target libgomp depend on target libstdc++-v3: circular
+# dependency.  We thus cannot instantiate 'AC_PROG_CXX' here: we'd get
+# '-funconfigured-libstdc++-v3' (see '../configure.ac').  Therefore, just
+# capture 'CXX', and we'll fix this up at 'make check' time (see
+# 'testsuite/lib/libgomp.exp:libgomp_init').
+AC_SUBST(CXX)
+
 # Create a spec file, so that compile/link tests don't fail
 test -f libgfortran.spec || touch libgfortran.spec
 FCFLAGS="$FCFLAGS -L."
 
-# We need gfortran to compile parts of the library
+# We need 'gfortran' to compile parts of the library, and test libgomp Fortran
+# support.
 # We can't use AC_PROG_FC because it expects a fully working gfortran.
 #AC_PROG_FC(gfortran)
 case `echo $GFORTRAN` in
diff --git a/libgomp/testsuite/Makefile.in b/libgomp/testsuite/Makefile.in
index 7a88f0fe5c6dc7de0326d2572bc00f42c1c174c2..4e8b21e8b38c751b13e43abc5030b84862df1635 100644
--- a/libgomp/testsuite/Makefile.in
+++ b/libgomp/testsuite/Makefile.in
@@ -147,6 +147,7 @@ CFLAGS = @CFLAGS@
 CPP = @CPP@
 CPPFLAGS = @CPPFLAGS@
 CPU_COUNT = @CPU_COUNT@
+CXX = @CXX@
 CYGPATH_W = @CYGPATH_W@
 DEFS = @DEFS@
 DEPDIR = @DEPDIR@
diff --git a/libgomp/testsuite/lib/libgomp.exp b/libgomp/testsuite/lib/libgomp.exp
index 48c43e4a136e39557a6be51f16739ceaf67c6c74..30d67e4f280a49dca5926aea85da5e1647b86127 100644
--- a/libgomp/testsuite/lib/libgomp.exp
+++ b/libgomp/testsuite/lib/libgomp.exp
@@ -66,7 +66,7 @@ proc libgomp_init { args } {
     global ALWAYS_CFLAGS
     global CFLAGS
     global TOOL_EXECUTABLE TOOL_OPTIONS
-    global GCC_UNDER_TEST
+    global GCC_UNDER_TEST GXX_UNDER_TEST GFORTRAN_UNDER_TEST
     global TESTING_IN_BUILD_TREE
     global target_triplet
     global always_ld_library_path
@@ -85,12 +85,45 @@ proc libgomp_init { args } {
       setenv LANG C.ASCII
     }
 
+    if { $blddir != "" } {
+	# Fix up '-funconfigured-libstdc++-v3' in 'GXX_UNDER_TEST' (see
+	# '../../configure.ac').
+	set flags_file "${blddir}/../libstdc++-v3/scripts/testsuite_flags"
+	if { [file exists $flags_file] } {
+	    set flags [exec sh $flags_file --build-includes]
+	    verbose -log "GXX_UNDER_TEST = $GXX_UNDER_TEST"
+	    set GXX_UNDER_TEST [string map [list \
+	      " -funconfigured-libstdc++-v3 " " $flags " \
+					   ] $GXX_UNDER_TEST]
+	    verbose -log "GXX_UNDER_TEST = $GXX_UNDER_TEST"
+	}
+    }
     if ![info exists GCC_UNDER_TEST] then {
 	if [info exists TOOL_EXECUTABLE] {
 	    set GCC_UNDER_TEST $TOOL_EXECUTABLE
 	} else {
 	    set GCC_UNDER_TEST "[find_gcc]"
 	}
+	# Only if we're guessing 'GCC_UNDER_TEST', we're also going to guess
+	# 'GXX_UNDER_TEST', 'GFORTRAN_UNDER_TEST'.
+	if ![info exists GXX_UNDER_TEST] then {
+	    if [info exists TOOL_EXECUTABLE] {
+		set GXX_UNDER_TEST $TOOL_EXECUTABLE
+	    } else {
+		set GXX_UNDER_TEST "[find_g++]"
+	    }
+	} else {
+	    error "GXX_UNDER_TEST set but not GCC_UNDER_TEST"
+	}
+	if ![info exists GFORTRAN_UNDER_TEST] then {
+	    if [info exists TOOL_EXECUTABLE] {
+		set GFORTRAN_UNDER_TEST $TOOL_EXECUTABLE
+	    } else {
+		set GFORTRAN_UNDER_TEST "[find_gfortran]"
+	    }
+	} else {
+	    error "GFORTRAN_UNDER_TEST set but not GCC_UNDER_TEST"
+	}
     }
 
     if ![info exists tmpdir] {
diff --git a/libgomp/testsuite/libgomp-site-extra.exp.in b/libgomp/testsuite/libgomp-site-extra.exp.in
index c0d26660bad96f7fcaba6a9073f7fadfff685c0e..15407ab1749ffff0aef38eea36146f32d8c5c19e 100644
--- a/libgomp/testsuite/libgomp-site-extra.exp.in
+++ b/libgomp/testsuite/libgomp-site-extra.exp.in
@@ -1 +1,3 @@
 set GCC_UNDER_TEST {@CC@}
+set GXX_UNDER_TEST {@CXX@}
+set GFORTRAN_UNDER_TEST {@FC@}
diff --git a/libgomp/testsuite/libgomp.c++/c++.exp b/libgomp/testsuite/libgomp.c++/c++.exp
index 4110ada7c46e6c305c600cedef18c63d58c8b8d9..8b4563b1d0def4e22fcdd21a9c89cbeaa5b6dc3b 100644
--- a/libgomp/testsuite/libgomp.c++/c++.exp
+++ b/libgomp/testsuite/libgomp.c++/c++.exp
@@ -15,12 +15,7 @@ if { $blddir != "" } {
     verbose -log "GXX_UNDER_TEST not defined, will not execute c++ tests"
     return
 }
-set lang_link_flags "-lstdc++"
-# Switch into C++ mode.  Otherwise, the 'libgomp.c-c++-common/*.c'
-# files would be compiled as C files.
-set SAVE_GCC_UNDER_TEST "$GCC_UNDER_TEST"
-set GCC_UNDER_TEST "$GCC_UNDER_TEST -x c++"
-lappend ALWAYS_CFLAGS "compiler=$GCC_UNDER_TEST"
+lappend ALWAYS_CFLAGS "compiler=$GXX_UNDER_TEST"
 
 # If a testcase doesn't have special options, use these.
 if ![info exists DEFAULT_CFLAGS] then {
@@ -45,28 +40,13 @@ if { $blddir != "" } {
 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
 set_ld_library_path_env_vars
 
-set flags_file "${blddir}/../libstdc++-v3/scripts/testsuite_flags"
-if { $blddir != ""
-     && [file exists $flags_file] } {
-    set lang_source_re {^.*\.[cC]$}
-    set lang_include_flags [exec sh $flags_file --build-includes]
-}
-
 # Main loop.
 dg-runtest $tests "" $DEFAULT_CFLAGS
 
-# See above.
-set GCC_UNDER_TEST "$SAVE_GCC_UNDER_TEST"
-
-if [info exists lang_include_flags] then {
-    unset lang_source_re
-    unset lang_include_flags
-}
 if { $blddir != "" } {
     unset libstdc++_library_path
     unset lang_library_paths
 }
-unset lang_link_flags
 
 # All done.
 dg-finish
diff --git a/libgomp/testsuite/libgomp.fortran/fortran.exp b/libgomp/testsuite/libgomp.fortran/fortran.exp
index 9aeebd3af7cdc42c7a52550b4377f3ff2fd01b48..e69656bce23ba99e71d7615fb6bb1bbae61b6bc4 100644
--- a/libgomp/testsuite/libgomp.fortran/fortran.exp
+++ b/libgomp/testsuite/libgomp.fortran/fortran.exp
@@ -28,8 +28,8 @@ if { $blddir != "" } {
     set lang_source_re {^.*\.[fF](|90|95|03|08)$}
     set lang_include_flags "-fintrinsic-modules-path=${blddir}"
 }
-set lang_link_flags "-lgfortran -foffload=-lgfortran"
-lappend ALWAYS_CFLAGS "compiler=$GCC_UNDER_TEST"
+set lang_link_flags "-foffload=-lgfortran"
+lappend ALWAYS_CFLAGS "compiler=$GFORTRAN_UNDER_TEST"
 
 # Initialize dg.
 dg-init
@@ -46,13 +46,6 @@ if { $blddir != "" } {
 
     if { $libquadmath_library_path != "" } {
 	append ld_library_path ":${blddir}/${libquadmath_library_path}"
-	append lang_link_flags " -lquadmath"
-    }
-} else {
-    if { [check_no_compiler_messages has_libquadmath executable {
-             int main() {return 0;}
-          } "-lgfortran -lquadmath"] } then {
-	append lang_link_flags " -lquadmath"
     }
 }
 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
diff --git a/libgomp/testsuite/libgomp.oacc-c++/c++.exp b/libgomp/testsuite/libgomp.oacc-c++/c++.exp
index 70fb9da0ef4e4bc66ee581b6b695936b4d3d2791..79df401c99ab74267e4a3b20109b80bba25c97a9 100644
--- a/libgomp/testsuite/libgomp.oacc-c++/c++.exp
+++ b/libgomp/testsuite/libgomp.oacc-c++/c++.exp
@@ -25,12 +25,7 @@ if { $blddir != "" } {
     verbose -log "GXX_UNDER_TEST not defined, will not execute c++ tests"
     return
 }
-set lang_link_flags "-lstdc++"
-# Switch into C++ mode.  Otherwise, the 'libgomp.oacc-c-c++-common/*.c'
-# files would be compiled as C files.
-set SAVE_GCC_UNDER_TEST "$GCC_UNDER_TEST"
-set GCC_UNDER_TEST "$GCC_UNDER_TEST -x c++"
-lappend ALWAYS_CFLAGS "compiler=$GCC_UNDER_TEST"
+lappend ALWAYS_CFLAGS "compiler=$GXX_UNDER_TEST"
 
 # Initialize dg.
 dg-init
@@ -51,13 +46,6 @@ if { $blddir != "" } {
 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]
 set_ld_library_path_env_vars
 
-set flags_file "${blddir}/../libstdc++-v3/scripts/testsuite_flags"
-if { $blddir != ""
-     && [file exists $flags_file] } {
-    set lang_source_re {^.*\.[cC]$}
-    set lang_include_flags [exec sh $flags_file --build-includes]
-}
-
 # Test with all available offload targets, and with offloading disabled.
 foreach offload_target [concat [split $offload_targets ","] "disable"] {
     global openacc_device_type
@@ -132,18 +120,10 @@ foreach offload_target [concat [split $offload_targets ","] "disable"] {
 }
 unset offload_target
 
-# See above.
-set GCC_UNDER_TEST "$SAVE_GCC_UNDER_TEST"
-
-if [info exists lang_include_flags] then {
-    unset lang_source_re
-    unset lang_include_flags
-}
 if { $blddir != "" } {
     unset libstdc++_library_path
     unset lang_library_paths
 }
-unset lang_link_flags
 
 # All done.
 torture-finish
diff --git a/libgomp/testsuite/libgomp.oacc-fortran/fortran.exp b/libgomp/testsuite/libgomp.oacc-fortran/fortran.exp
index 56dd9485339009ecba1540aa401de9554521e3b7..2e8aa2285fbaa07d17b6deb77f04406c6af68343 100644
--- a/libgomp/testsuite/libgomp.oacc-fortran/fortran.exp
+++ b/libgomp/testsuite/libgomp.oacc-fortran/fortran.exp
@@ -30,8 +30,8 @@ if { $blddir != "" } {
     set lang_source_re {^.*\.[fF](|90|95|03|08)$}
     set lang_include_flags "-fintrinsic-modules-path=${blddir}"
 }
-set lang_link_flags "-lgfortran -foffload=-lgfortran"
-lappend ALWAYS_CFLAGS "compiler=$GCC_UNDER_TEST"
+set lang_link_flags "-foffload=-lgfortran"
+lappend ALWAYS_CFLAGS "compiler=$GFORTRAN_UNDER_TEST"
 
 # Initialize dg.
 dg-init
@@ -49,13 +49,6 @@ if { $blddir != "" } {
 
     if { $libquadmath_library_path != "" } {
 	append ld_library_path ":${blddir}/${libquadmath_library_path}"
-	append lang_link_flags " -lquadmath"
-    }
-} else {
-    if { [check_no_compiler_messages has_libquadmath executable {
-             int main() {return 0;}
-          } "-lgfortran -lquadmath"] } then {
-	append lang_link_flags " -lquadmath"
     }
 }
 append ld_library_path [gcc-set-multilib-library-path $GCC_UNDER_TEST]