【问题标题】:Cannot use C libraries that include math.h with g++ 7 (Raspberry PI)不能将包含 math.h 的 C 库与 g++ 7 (Raspberry PI) 一起使用
【发布时间】:2018-07-08 21:33:44
【问题描述】:

我已经为 Raspberry PI 构建了 GCC 7.2.0,并使用前缀 /usr/local/gcc-7.2.0(使用 this tutorial)安装了它。每当我尝试编译包含 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__));
 ^

【问题讨论】:

  • 如果用-std=gnu++98编译会怎样?
  • @atomSmasher 是的。我确实需要 c++11 功能:(

标签: c++ raspberry-pi g++ math.h gcc7


【解决方案1】:

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

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

【讨论】:

  • 我的代码中没有using namespace std;,也没有使用任何提到的数学函数。不幸的是,我无法控制我正在使用的库中使用的内容,因为它们是第三方包。
  • 我明白了。所以我的 glibc 版本是旧的。我没有那个补丁
猜你喜欢
  • 2021-12-01
  • 2016-02-22
  • 2017-09-27
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多