Cygwin环境下pip安装SciPy出现编译错误求解决方案
在Cygwin环境下pip安装SciPy时的编译错误解决方案
问题背景
在Cygwin环境中通过pip安装SciPy时触发编译错误,核心问题是代码中引用的M_SQRT1_2、M_PI等数学宏未被编译器识别,且无法修改SciPy源码,需通过编译参数调整解决。
错误日志
2023-02-11T17:48:15,190 [250/1620] Compiling C++ object scipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p/_faddeeva.cxx.o 2023-02-11T17:48:15,191 FAILED: scipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p/_faddeeva.cxx.o 2023-02-11T17:48:15,192 c++ -Iscipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p -Iscipy/special -I../../scipy/special -I/tmp/pip-build-env-m1hcjhav/overlay/lib/python3.9/site-packages/numpy/core/include -I../../scipy/_lib/boost -Iscipy/_lib -I../../scipy/_lib -I../../scipy/_build_utils/src -I/usr/include/python3.9 -fvisibility=hidden -fvisibility-inlines-hidden -fdiagnostics-color=always -D_FILE_OFFSET_BITS=64 -Wall -Winvalid-pch -std=c++14 -O2 -Wno-cpp -MD -MQ scipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p/_faddeeva.cxx.o -MF scipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p/_faddeeva.cxx.o.d -o scipy/special/_ufuncs_cxx.cpython-39-x86_64-cygwin.dll.p/_faddeeva.cxx.o -c ../../scipy/special/_faddeeva.cxx 2023-02-11T17:48:15,193 ../../scipy/special/_faddeeva.cxx: In function ‘npy_cdouble faddeeva_ndtr(npy_cdouble)’: 2023-02-11T17:48:15,194 ../../scipy/special/_faddeeva.cxx:79:10: error: ‘M_SQRT1_2’ was not declared in this scope; did you mean ‘NPY_SQRT1_2’? 2023-02-11T17:48:15,196 79 | z *= M_SQRT1_2; 2023-02-11T17:48:15,199 | ^~~~~~~~~ 2023-02-11T17:48:15,200 | NPY_SQRT1_2 2023-02-11T17:48:15,201 ../../scipy/special/_faddeeva.cxx: In function ‘double faddeeva_log_ndtr(double)’: 2023-02-11T17:48:15,202 ../../scipy/special/_faddeeva.cxx:108:18: error: ‘M_SQRT1_2’ was not declared in this scope; did you mean ‘NPY_SQRT1_2’? 2023-02-11T17:48:15,203 108 | double t = x*M_SQRT1_2; 2023-02-11T17:48:15,204 | ^~~~~~~~~ 2023-02-11T17:48:15,205 | NPY_SQRT1_2 2023-02-11T17:48:15,206 ../../scipy/special/_faddeeva.cxx: In function ‘npy_cdouble faddeeva_log_ndtr_complex(npy_cdouble)’: 2023-02-11T17:48:15,207 ../../scipy/special/_faddeeva.cxx:130:53: error: ‘M_SQRT1_2’ was not declared in this scope; did you mean ‘NPY_SQRT1_2’? 2023-02-11T17:48:15,208 130 | complex<double> w = -0.5 * Faddeeva::erfc(z*M_SQRT1_2); 2023-02-11T17:48:15,209 | ^~~~~~~~~ 2023-02-11T17:48:15,210 | NPY_SQRT1_2 2023-02-11T17:48:15,211 ../../scipy/special/_faddeeva.cxx:136:11: error: ‘M_SQRT1_2’ was not declared in this scope; did you mean ‘NPY_SQRT1_2’? 2023-02-11T17:48:15,212 136 | z *= -M_SQRT1_2; 2023-02-11T17:48:15,218 | ^~~~~~~~~ 2023-02-11T17:48:15,219 | NPY_SQRT1_2 2023-02-11T17:48:15,220 ../../scipy/special/_faddeeva.cxx: In function ‘double faddeeva_log_ndtr_complex(npy_cdouble)’: 2023-02-11T17:48:15,221 146 | double im = fmod(mIm_z2, 2.0*M_PI); 2023-02-11T17:48:15,223 | ^~~~ 2023-02-11T17:48:15,224 ../../scipy/special/_faddeeva.cxx: In function ‘double faddeeva_voigt_profile(double, double, double)’: 2023-02-11T17:48:15,225 ../../scipy/special/_faddeeva.cxx:177:24: error: ‘M_PI’ was not declared in this scope 2023-02-11T17:48:15,226 177 | return gamma / M_PI / (x*x + gamma*gamma); 2023-02-11T17:48:15,227 | ^~~~
解决方案
方案1:通过环境变量定义缺失的宏
直接在pip安装时传递编译参数,让编译器自动识别这些数学宏:
CXXFLAGS="-DM_SQRT1_2=1/sqrt(2) -DM_PI=3.141592653589793" pip install scipy
如果需要持久化这个环境变量,可以先执行:
export CXXFLAGS="-DM_SQRT1_2=1/sqrt(2) -DM_PI=3.141592653589793"
再执行pip install scipy。
方案2:使用Cygwin包管理器安装预编译版本
跳过源码编译步骤,直接用Cygwin自带的包管理器安装现成的SciPy:
- 如果没有
apt-cyg,先安装它:wget https://raw.githubusercontent.com/transcode-open/apt-cyg/master/apt-cyg install apt-cyg /usr/local/bin chmod +x /usr/local/bin/apt-cyg - 安装SciPy:
apt-cyg install python3-scipy
内容的提问来源于stack exchange,提问作者natan
相关产品推荐
相关产品推荐

