不能将包含 math.h 的 C 库与 g++ 7 (Raspberry PI) 一起使用

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

我已经为 Raspberry PI 构建了 GCC 7.2.0,并使用前缀 /usr/local/gcc-7.2.0(使用 this tutorial)安装了它。每当我尝试编译包含 C 库的源代码时,C 库又包含 math.h,我都会收到奇怪的错误。下面是一个最小的例子:

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

int main() {
    return 0;
}

注:extern "C"这里是必须的,因为avcodec.h没有。我确定这不是问题所在,因为我已尝试将其删除,但仍然出现相同的错误。

使用 /usr/local/gcc-7.2.0/bin/g++-7.2.0 main.cpp 编译时,出现以下错误:

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__));
 ^

您有全局命名空间冲突,因为 math.h 在全局命名空间中声明了 C 名称。

问题出在您的 mathcalls.h 版本中 有一个补丁通过在使用 C++11 或更新版本时有条件地不定义 isnanisinf 来修复它。 Here is the patch for mathcalls.h