【原创】cmath:1157:11: error: '::hypot' has not been declared


问题简述:

在某一项目 pyconfig 的 header file中遇到的问题,代码运行报错“cmath:1157:11: error: '::hypot' has not been declared”。

看源码可以看出导致问题出现的原因是:hypot 被某段代码重命名为了 _hypot,致使 cmath 调用  hypot 的时候无法找到


解决方法:

1、源码修改(哎,经常遇到问题找不到很好的解决方法,只能搞别人的源码)

修改源码中的引入顺序,让include “math.h”的引入在include “Python”之前,让cmath在hypot被重命名之前调用hypot。

具体做法为:

方法1、修改Python.h的源码,在它的开头加上math.h的引入,修改结果如下所示

#ifndef Py_PYTHON_H
#define Py_PYTHON_H
/* Since this is a "meta-include" file, no #ifdef __cplusplus / extern "C" { */

/* Include nearly all Python header files */
#include "math.h"

方法2、调换include “math.h”和include “Python”的引入顺序。

2、建立自己的math.h(为了尽量避免修改别人源码,复制一份源码专门给自己用)

  1. 复制源码中的math.h改名为mathXXX.h
  2. 把mathXXX.h中的“hypot”被替换为“_hypot”
  3. 把项目中需要调用math.h的地方(我的项目中是pyport.h这个文件调用math.h的)改为调用mathXXX.h

3、编译选项中添加-D_hypot=hypot(后来通过limingaoyu博客了解,本人未证实)

详情见https://blog.csdn.net/limingaoyu/article/details/101424262

4、在cmath头部添加#define _hypot hypot(后来通过gdtop818博客了解,本人未证实)

这个针对的报错是cmath:1096:11:error: '::hypot' has not been declared,与本文问题可能有差异,未确定是否能跑通。

详情见https://blog.csdn.net/weixin_37993251/article/details/88054384

你可能感兴趣的:(Python)