Ubuntu18.04+Python3.6安装trm.pgplot时wheel构建失败求助
在Ubuntu 18.04系统、Python 3.6环境下,执行以下命令安装trm.pgplot包时:
pip3 install . --user
遇到链接错误,关键报错内容如下:
/usr/bin/ld: /usr/local/pgplot//libpgplot.a(xwdriv.o): 针对符号`stderr@@GLIBC_2.2.5'的重定位R_X86_64_PC32无法用于创建共享对象;请使用-fPIC重新编译
/usr/bin/ld: 最终链接失败:无效值
collect2: error: ld返回状态1
error: 命令'x86_64-linux-gnu-gcc'执行失败,退出状态为1
完整错误输出:
Processing /home/chinmay/trm-pgplot
Building wheels for collected packages: trm.pgplot
Running setup.py bdist_wheel for trm.pgplot ... error
Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;file='/tmp/pip-b_8sag87-build/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" bdist_wheel -d /tmp/tmp6x5jsk6hpip-wheel- --python-tag cp36:
running bdist_wheel
running build
running build_py
package init file 'trm/init.py' not found (or not a regular file)
creating build
creating build/lib.linux-x86_64-3.6
creating build/lib.linux-x86_64-3.6/trm
creating build/lib.linux-x86_64-3.6/trm/pgplot
copying trm/pgplot/init.py -> build/lib.linux-x86_64-3.6/trm/pgplot
running build_ext
building 'trm.pgplot._pgplot' extension
creating build/temp.linux-x86_64-3.6
creating build/temp.linux-x86_64-3.6/trm
creating build/temp.linux-x86_64-3.6/trm/pgplot
x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DMAJOR_VERSION=0 -DMINOR_VERSION=1 -I/home/chinmay/.local/lib/python3.6/site-packages/numpy/core/include -I/usr/local/pgplot/ -I/usr/include/python3.6m -c trm/pgplot/_pgplot.c -o build/temp.linux-x86_64-3.6/trm/pgplot/_pgplot.o
In file included from /home/chinmay/.local/lib/python3.6/site-packages/numpy/core/include/numpy/ndarraytypes.h:1822:0,
from /home/chinmay/.local/lib/python3.6/site-packages/numpy/core/include/numpy/ndarrayobject.h:12,
from /home/chinmay/.local/lib/python3.6/site-packages/numpy/core/include/numpy/arrayobject.h:4,
from trm/pgplot/_pgplot.c:791:
/home/chinmay/.local/lib/python3.6/site-packages/numpy/core/include/numpy/npy_1_7_deprecated_api.h:17:2:
warning: #warning "Using deprecated NumPy API, disable it with "
"#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION" [-Wcpp]
#warning "Using deprecated NumPy API, disable it with "
^~~~~~~
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-Bsymbolic-functions -Wl,-z,relro -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 build/temp.linux-x86_64-3.6/trm/pgplot/_pgplot.o -L/usr/X11R6/lib
-L/opt/local/lib -L/usr/local/pgplot/ -lcpgplot -lpgplot -lX11 -lm -lgfortran -lpng -lz -o build/lib.linux-x86_64-3.6/trm/pgplot/_pgplot.cpython-36m-x86_64-linux-gnu.so
/usr/bin/ld: /usr/local/pgplot//libpgplot.a(xwdriv.o): 针对符号`stderr@@GLIBC_2.2.5'的重定位R_X86_64_PC32无法用于创建共享对象;请使用-fPIC重新编译
/usr/bin/ld: 最终链接失败:无效值
collect2: error: ld返回状态1
error: 命令'x86_64-linux-gnu-gcc'执行失败,退出状态为1
问题根源是系统中安装的libpgplot.a静态库未使用-fPIC参数编译,导致无法链接生成Python共享扩展模块。按以下步骤解决:
- 卸载现有pgplot
如果之前是通过预编译包或包管理器安装的pgplot,先移除:
sudo rm -rf /usr/local/pgplot/
- 从源码编译带-fPIC的pgplot
- 下载源码并解压:
wget http://www.astro.caltech.edu/~tjp/pgplot/pgplot5.2.tar.gz tar xzf pgplot5.2.tar.gz cd pgplot
- 修改编译配置,添加
-fPIC参数:
cp sys_linux.dat sys_linux_pic.dat sed -i 's/FFLAGS = -O/FFLAGS = -O -fPIC/' sys_linux_pic.dat sed -i 's/CFLAGS = -O/CFLAGS = -O -fPIC/' sys_linux_pic.dat
- 编译并安装:
mkdir /usr/local/pgplot ./makemake linux gfortran make all make install
- 重新安装trm.pgplot
回到trm-pgplot源码目录,重新执行安装命令:
pip3 install . --user
- 编译pgplot前确保安装依赖:
sudo apt install gfortran libx11-dev libpng-dev - NumPy的deprecated API警告不影响安装成功,若要消除可修改trm.pgplot的setup.py,在引入numpy前添加
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
内容的提问来源于stack exchange,提问作者Keith Bridges

