Error building Boost 1.49.0 with GCC 4.7.0

As far as I can see this happens when compiling with MingW, using -std=c++0xx, and including Python.h before cmath. And note that cmath is included by quite a few other header files... Note that the problem is not Boost specific. Complicating fact is that in my standard MingW - Visual Studio cross compilation setup, Visual Studio 2010 needs in Debug mode to have Python.h included before many other standard include files. Solution is to include cmath first, followed by Python.h, so you get code like:

#include <cmath>
#include <Python.h>
#include < other standard headers >

The answer by @Praetorian correctly identifies the problem. On the other hand, the Python headers are technically meant to come before any others. In addition, sometimes the accepted solution does not work or is inconvenient in the build system, so I came up with an alternate solution. Add the following flag to the call to g++:

-D_hypot=hypot

This makes it so that the harmful macro in the Python headers becomes a no-op, and the compilation error goes away.


The problem is identified correctly by @Praetorian.

In my case it only appears in one file.So I simply add

#define _hypot hypot before #include <Python.h>

and works.

Hope this can be enlightening.


Found the answer in this forum post. It seems that pyconfig.h has the following lines:

#if defined(__GNUC__) && defined(_WIN32)
// ...
#define hypot _hypot
// ...
#endif /* GNUC */

but cmath included with MinGW expects the function to be named hypot and not _hypot, which causes the compilation errors.

The fix was to include the following to my bjam command line's cxxflags option

bjam ... cxxflags="-include cmath "

This indicates that g++ should include the cmath header at the beginning of every source file.

Tags:

C++

Boost

Mingw