diff --git a/config/libstdc++-raw-cxx.m4 b/config/libstdc++-raw-cxx.m4
new file mode 100644
index 0000000..453153c
--- /dev/null
+++ b/config/libstdc++-raw-cxx.m4
@@ -0,0 +1,26 @@
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 3, or (at your option) any later
+# version.
+#
+# GCC is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+# for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3.  If not see
+# <http://www.gnu.org/licenses/>.
+
+# Define compiler flags, LIBSTDCXX_RAW_CXX_CXXFLAGS, for libstdc++-v3
+# header files to compile libraries in C++ with raw_cxx=true.
+AC_DEFUN([GCC_LIBSTDCXX_RAW_CXX_CXXFLAGS], [
+  AC_REQUIRE([ACX_NONCANONICAL_TARGET])
+  LIBSTDCXX_RAW_CXX_CXXFLAGS="\
+    -I\$(top_builddir)/../libstdc++-v3/include \
+    -I\$(top_builddir)/../libstdc++-v3/include/\$(target_noncanonical) \
+    -I\$(top_srcdir)/../libstdc++-v3/libsupc++"
+  AC_SUBST(LIBSTDCXX_RAW_CXX_CXXFLAGS)
+])
diff --git a/libsanitizer/Makefile.am b/libsanitizer/Makefile.am
index 9a06839..308d438 100644
--- a/libsanitizer/Makefile.am
+++ b/libsanitizer/Makefile.am
@@ -41,8 +41,6 @@ AM_MAKEFLAGS = \
 	"includedir=$(includedir)" \
 	"AR=$(AR)" \
 	"AS=$(AS)" \
-	"CC=$(CC)" \
-	"CXX=$(CXX)" \
 	"LD=$(LD)" \
 	"LIBCFLAGS=$(LIBCFLAGS)" \
 	"NM=$(NM)" \
diff --git a/libsanitizer/asan/Makefile.am b/libsanitizer/asan/Makefile.am
index 7656f9f..59327a0 100644
--- a/libsanitizer/asan/Makefile.am
+++ b/libsanitizer/asan/Makefile.am
@@ -8,6 +8,7 @@ if USING_MAC_INTERPOSE
 DEFS += -DMAC_INTERPOSE_FUNCTIONS -DMISSING_BLOCKS_SUPPORT
 endif
 AM_CXXFLAGS = -Wall -W -Wno-unused-parameter -Wwrite-strings
-pedantic -Wno-long-long  -fPIC -fno-builtin -fno-exceptions
-fomit-frame-pointer -funwind-tables -fvisibility=hidden
-Wno-variadic-macros -Wno-c99-extensions
+AM_CXXFLAGS += $(LIBSTDCXX_RAW_CXX_CXXFLAGS)
 ACLOCAL_AMFLAGS = -I $(top_srcdir) -I $(top_srcdir)/config

 toolexeclib_LTLIBRARIES = libasan.la
@@ -73,8 +74,6 @@ AM_MAKEFLAGS = \
 	"includedir=$(includedir)" \
 	"AR=$(AR)" \
 	"AS=$(AS)" \
-	"CC=$(CC)" \
-	"CXX=$(CXX)" \
 	"LD=$(LD)" \
 	"LIBCFLAGS=$(LIBCFLAGS)" \
 	"NM=$(NM)" \
diff --git a/libsanitizer/configure.ac b/libsanitizer/configure.ac
index 30614e5..4090e6c 100644
--- a/libsanitizer/configure.ac
+++ b/libsanitizer/configure.ac
@@ -21,6 +21,7 @@ AC_MSG_RESULT($version_specific_libs)
 AC_CANONICAL_SYSTEM
 target_alias=${target_alias-$host_alias}
 AC_SUBST(target_alias)
+GCC_LIBSTDCXX_RAW_CXX_CXXFLAGS

 AM_INIT_AUTOMAKE(foreign)
 AM_ENABLE_MULTILIB(, ..)
diff --git a/libsanitizer/interception/Makefile.am
b/libsanitizer/interception/Makefile.am
index a815dcf..6ae59b5 100644
--- a/libsanitizer/interception/Makefile.am
+++ b/libsanitizer/interception/Makefile.am
@@ -5,6 +5,7 @@ gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER)

 DEFS = -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
 AM_CXXFLAGS = -Wall -W -Wno-unused-parameter -Wwrite-strings
-pedantic -Wno-long-long  -fPIC -fno-builtin -fno-exceptions
-fomit-frame-pointer -funwind-tables -fvisibility=hidden
-Wno-variadic-macros -Wno-c99-extensions
+AM_CXXFLAGS += $(LIBSTDCXX_RAW_CXX_CXXFLAGS)
 ACLOCAL_AMFLAGS = -I m4

 noinst_LTLIBRARIES = libinterception.la
@@ -47,8 +48,6 @@ AM_MAKEFLAGS = \
 	"includedir=$(includedir)" \
 	"AR=$(AR)" \
 	"AS=$(AS)" \
-	"CC=$(CC)" \
-	"CXX=$(CXX)" \
 	"LD=$(LD)" \
 	"LIBCFLAGS=$(LIBCFLAGS)" \
 	"NM=$(NM)" \
diff --git a/libsanitizer/sanitizer_common/Makefile.am
b/libsanitizer/sanitizer_common/Makefile.am
index 2968c05..708b2a4 100644
--- a/libsanitizer/sanitizer_common/Makefile.am
+++ b/libsanitizer/sanitizer_common/Makefile.am
@@ -5,6 +5,7 @@ gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER)

 DEFS = -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
 AM_CXXFLAGS = -Wall -W -Wno-unused-parameter -Wwrite-strings
-pedantic -Wno-long-long  -fPIC -fno-builtin -fno-exceptions
-fomit-frame-pointer -funwind-tables -fvisibility=hidden
-Wno-variadic-macros -Wno-c99-extensions
+AM_CXXFLAGS += $(LIBSTDCXX_RAW_CXX_CXXFLAGS)
 ACLOCAL_AMFLAGS = -I m4

 noinst_LTLIBRARIES = libsanitizer_common.la
@@ -59,8 +60,6 @@ AM_MAKEFLAGS = \
 	"includedir=$(includedir)" \
 	"AR=$(AR)" \
 	"AS=$(AS)" \
-	"CC=$(CC)" \
-	"CXX=$(CXX)" \
 	"LD=$(LD)" \
 	"LIBCFLAGS=$(LIBCFLAGS)" \
 	"NM=$(NM)" \
diff --git a/libsanitizer/tsan/Makefile.am b/libsanitizer/tsan/Makefile.am
index a391986..867c152 100644
--- a/libsanitizer/tsan/Makefile.am
+++ b/libsanitizer/tsan/Makefile.am
@@ -5,6 +5,7 @@ gcc_version := $(shell cat $(top_srcdir)/../gcc/BASE-VER)

 DEFS = -D_GNU_SOURCE -D_DEBUG -D__STDC_CONSTANT_MACROS
-D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
 AM_CXXFLAGS = -Wall -W -Wno-unused-parameter -Wwrite-strings
-pedantic -Wno-long-long  -fPIC -fno-builtin -fno-exceptions
-fomit-frame-pointer -funwind-tables -fvisibility=hidden
-Wno-variadic-macros
+AM_CXXFLAGS += $(LIBSTDCXX_RAW_CXX_CXXFLAGS)
 ACLOCAL_AMFLAGS = -I m4

 toolexeclib_LTLIBRARIES = libtsan.la
@@ -67,8 +68,6 @@ AM_MAKEFLAGS = \
 	"includedir=$(includedir)" \
 	"AR=$(AR)" \
 	"AS=$(AS)" \
-	"CC=$(CC)" \
-	"CXX=$(CXX)" \
 	"LD=$(LD)" \
 	"LIBCFLAGS=$(LIBCFLAGS)" \
 	"NM=$(NM)" \
