Since we poison "malloc" and friends in system.h, any C++ code that
includes a standard library header such as <algorithm>, which in turn
includes <cstdlib>, will get poisoning errors due to lines like
#undef malloc
using ::malloc;
The solution is to include <cstdlib> before poisoning those identifiers.
Oleg posted a variant of this patch last year, which included <cstdlib>
instead of <stdlib.h>; my patch includes both to avoid issues with other
library implementations in which <cstdlib> might not declare functions
in the global namespace.
Tested x86_64-pc-linux-gnu, applying to trunk.
commit 33be921c304a8bf38bbf5ae30b3489a01be4763c
Author: Jason Merrill <ja...@redhat.com>
Date: Mon Jun 10 21:17:00 2013 -0400
* system.h: Include <cstdlib> as well as <stdlib.h>.
diff --git a/gcc/system.h b/gcc/system.h
index 41cd565..f10ba4a 100644
--- a/gcc/system.h
+++ b/gcc/system.h
@@ -226,6 +226,14 @@ extern int errno;
# include <stdlib.h>
#endif
+/* When compiling C++ we need to include <cstdlib> as well as <stdlib.h> so
+ that it is processed before we poison "malloc"; otherwise, if a source
+ file uses a standard library header that includes <cstdlib>, we will get
+ an error about 'using std::malloc'. */
+#ifdef __cplusplus
+#include <cstdlib>
+#endif
+
/* Undef vec_free from AIX stdlib.h header which conflicts with vec.h. */
#undef vec_free