c++raspberry-pig++math.hgcc7

Cannot use C libraries that include math.h with g++ 7 (Raspberry PI)


I've built GCC 7.2.0 for Raspberry PI, and installed it with prefix /usr/local/gcc-7.2.0 (using this tutorial). Whenever I try to compile a source that includes a C library that in turn includes math.h I get strange errors. Below is a minimal example:

extern "C" {
#include <libavcodec/avcodec.h>
}

int main() {
    return 0;
}

Note: extern "C" is necessary here because avcodec.h doesn't have it. I'm sure it is not the problem, as I've tried removing it and still get the same error.

Compiling with /usr/local/gcc-7.2.0/bin/g++-7.2.0 main.cpp, I get the following error:

In file included from /usr/local/include/libavutil/common.h:36:0,
                 from /usr/local/include/libavutil/avutil.h:296,
                 from /usr/local/include/libavutil/samplefmt.h:24,
                 from /usr/local/include/libavcodec/avcodec.h:31,
                 from main.cpp:4:
/usr/local/gcc-7.2.0/include/c++/7.2.0/math.h:65:12: error: ‘constexpr bool std::isinf(double)’ conflicts with a previous declaration
 using std::isinf;
            ^~~~~
In file included from /usr/include/features.h:374:0,
                 from /usr/include/errno.h:28,
                 from /usr/local/include/libavcodec/avcodec.h:30,
                 from main.cpp:4:
/usr/include/arm-linux-gnueabihf/bits/mathcalls.h:201:1: note: previous declaration ‘int isinf(double)’
 __MATHDECL_1 (int,isinf,, (_Mdouble_ __value)) __attribute__ ((__const__));
 ^
In file included from /usr/local/include/libavutil/common.h:36:0,
                 from /usr/local/include/libavutil/avutil.h:296,
                 from /usr/local/include/libavutil/samplefmt.h:24,
                 from /usr/local/include/libavcodec/avcodec.h:31,
                 from main.cpp:4:
/usr/local/gcc-7.2.0/include/c++/7.2.0/math.h:66:12: error: ‘constexpr bool std::isnan(double)’ conflicts with a previous declaration
 using std::isnan;
            ^~~~~
In file included from /usr/include/features.h:374:0,
                 from /usr/include/errno.h:28,
                 from /usr/local/include/libavcodec/avcodec.h:30,
                 from main.cpp:4:
/usr/include/arm-linux-gnueabihf/bits/mathcalls.h:234:1: note: previous declaration ‘int isnan(double)’
 __MATHDECL_1 (int,isnan,, (_Mdouble_ __value)) __attribute__ ((__const__));
 ^

Solution

  • You have a global namespace collision because math.h declares C names in the global namespace.

    The problem is in your version of mathcalls.h There is a patch that fixes it by conditionally not defining isnan and isinf when using C++11 or newer. Here is the patch for mathcalls.h