Discussion:
[PATCH] Fix C++ compilation on MinGW
o***@gmail.com
2016-11-02 09:45:25 UTC
Permalink
From: Orgad Shaneh <***@gmail.com>

When compiling with MinGW, pyconfig.h renames hypot to _hypot.

If math.h is included later, hypot is not declared correctly.

When g++ is used, it has 'using ::hypot', which doesn't exist.
---
gdb/python/python-internal.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 8545c7b..9b43255 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -88,6 +88,10 @@
/* Request clean size types from Python. */
#define PY_SSIZE_T_CLEAN

+// math.h has to be included before Python.h, since pyconfig.h
+// renames hypot to _hypot, and this breaks MinGW compilation with g++.
+#include <math.h>
+
/* Include the Python header files using angle brackets rather than
double quotes. On case-insensitive filesystems, this prevents us
from including our python/python.h header file. */
--
2.10.1.windows.1
o***@gmail.com
2016-11-02 09:53:37 UTC
Permalink
From: Orgad Shaneh <***@gmail.com>

When compiling with MinGW, pyconfig.h renames hypot to _hypot.

If math.h is included later, hypot is not declared correctly.

When g++ is used, it has 'using ::hypot', which doesn't exist.
---
gdb/python/python-internal.h | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 8545c7b..5e80dce 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -88,6 +88,10 @@
/* Request clean size types from Python. */
#define PY_SSIZE_T_CLEAN

+/* math.h has to be included before Python.h, since pyconfig.h
+ renames hypot to _hypot, and this breaks MinGW compilation with g++. */
+#include <math.h>
+
/* Include the Python header files using angle brackets rather than
double quotes. On case-insensitive filesystems, this prevents us
from including our python/python.h header file. */
--
2.10.1.windows.1
Pedro Alves
2016-11-08 11:36:32 UTC
Permalink
Hi,
Post by o***@gmail.com
When compiling with MinGW, pyconfig.h renames hypot to _hypot.
Yeah, that's not a good idea in C++. We have a similar
issue with gnulib's '#define foo rpl_foo' preventing use of
"foo" as class methods etc. Particularly annoying for "open",
"close", etc. (I plan to address that.)
Post by o***@gmail.com
If math.h is included later, hypot is not declared correctly.
When g++ is used, it has 'using ::hypot', which doesn't exist.
So due to the define, that actually expanded to:

using ::_hypot;

Right?

Anyway, with your fix, IIUC, if someone adds a call to
std::hypot somewhere (or uses the "hypot" symbol for
something unrelated), we'll still have a problem.

I'm thinking that the patch below might be a better fix.
However, my cross mingw setup does not include Python, so I
can't verify it works.

WDYT?

From 49f5b6892c959e9f1a3ea50240e8d83da63c6268 Mon Sep 17 00:00:00 2001
From: Pedro Alves <***@redhat.com>
Date: Tue, 8 Nov 2016 10:48:32 +0000
Subject: [PATCH] Fix Python hypot mingw

---
gdb/python/python-internal.h | 12 ++++++++++++
1 file changed, 12 insertions(+)

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 8545c7b..9d1a046 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -88,6 +88,18 @@
/* Request clean size types from Python. */
#define PY_SSIZE_T_CLEAN

+/* When compiling with MinGW, pyconfig.h (included by other Python
+ headers) renames hypot to _hypot. That conflicts with C++'s
+ std::hypot. See <https://bugs.python.org/issue11566>.
+
+ In C++, it's not a good idea to define standard C functions as
+ macros. Fortunately, pyconfig.h has double-inclusion guards, so
+ include it ourselves, and #undef any conflicting symbol it may have
+ defined. Note libstdc++'s <c...> headers also #undef C-inherited
+ function-like macros, including "hypot". */
+#include <pyconfig.h>
+#undef hypot
+
/* Include the Python header files using angle brackets rather than
double quotes. On case-insensitive filesystems, this prevents us
from including our python/python.h header file. */
--
2.5.5
Orgad Shaneh
2016-11-13 21:48:14 UTC
Permalink
Post by Pedro Alves
Hi,
Post by o***@gmail.com
When compiling with MinGW, pyconfig.h renames hypot to _hypot.
Yeah, that's not a good idea in C++. We have a similar
issue with gnulib's '#define foo rpl_foo' preventing use of
"foo" as class methods etc. Particularly annoying for "open",
"close", etc. (I plan to address that.)
Post by o***@gmail.com
If math.h is included later, hypot is not declared correctly.
When g++ is used, it has 'using ::hypot', which doesn't exist.
using ::_hypot;
Right?
Anyway, with your fix, IIUC, if someone adds a call to
std::hypot somewhere (or uses the "hypot" symbol for
something unrelated), we'll still have a problem.
I'm thinking that the patch below might be a better fix.
However, my cross mingw setup does not include Python, so I
can't verify it works.
WDYT?
From 49f5b6892c959e9f1a3ea50240e8d83da63c6268 Mon Sep 17 00:00:00 2001
Date: Tue, 8 Nov 2016 10:48:32 +0000
Subject: [PATCH] Fix Python hypot mingw
---
gdb/python/python-internal.h | 12 ++++++++++++
1 file changed, 12 insertions(+)
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 8545c7b..9d1a046 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -88,6 +88,18 @@
/* Request clean size types from Python. */
#define PY_SSIZE_T_CLEAN
+/* When compiling with MinGW, pyconfig.h (included by other Python
+ headers) renames hypot to _hypot. That conflicts with C++'s
+ std::hypot. See <https://bugs.python.org/issue11566>.
+
+ In C++, it's not a good idea to define standard C functions as
+ macros. Fortunately, pyconfig.h has double-inclusion guards, so
+ include it ourselves, and #undef any conflicting symbol it may have
+ defined. Note libstdc++'s <c...> headers also #undef C-inherited
+ function-like macros, including "hypot". */
+#include <pyconfig.h>
+#undef hypot
+
/* Include the Python header files using angle brackets rather than
double quotes. On case-insensitive filesystems, this prevents us
from including our python/python.h header file. */
--
2.5.5
Yes, this looks fine. Thanks!

- Orgad

Loading...