gpt4 book ai didi

python - 为 python 设置 RPATH 不起作用

转载 作者:行者123 更新时间:2023-12-01 03:03:35 27 4
gpt4 key购买 nike

我尝试使用 patchelf 更改 python 的 RPATH 以指向本地安装的新更新的库。但 python 仍然无法找到新的库。

相反,如果我设置LD_LIBRARY_PATH环境变量,那么python就可以正常工作。

我正在执行以下操作 patchelf --set-rpath /home/user/new_lblas/ /home/user/.local/python

其中 new_lblas指向更新的软件包,如 intel-lapack、blas 等。

如果我现在这样做,python -c 'import numpy' ,它提示libblas.so.3gf未找到。但是,如果我设置环境变量 LD_LIBRARY_PATH=/home/user/new_lblas ,然后import numpy在 python 中工作正常。

有人可以帮我找出我犯了什么错误吗?

readelf -d /home/user/.local/python 的输出完成 patchelf 后如下所示

     Dynamic section at offset 0x270 contains 26 entries:
Tag Type Name/Value
0x000000000000001d (RUNPATH) Library runpath: [/home/user/new_lblas]
0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]
0x0000000000000001 (NEEDED) Shared library: [libutil.so.1]
0x0000000000000001 (NEEDED) Shared library: [librt.so.1]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000c (INIT) 0x41b700
0x000000000000000d (FINI) 0x569238
0x000000006ffffef5 (GNU_HASH) 0x4146b0
0x0000000000000005 (STRTAB) 0x3ff460
0x0000000000000006 (SYMTAB) 0x4085c0
0x000000000000000a (STRSZ) 37214 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000015 (DEBUG) 0x0
0x0000000000000003 (PLTGOT) 0x7f9fe8
0x0000000000000002 (PLTRELSZ) 8040 (bytes)

在执行 patchelf 之前,ELF header 如下所示

    Dynamic section at offset 0x1f9e00 contains 25 entries:                                           
Tag Type Name/Value
0x0000000000000001 (NEEDED) Shared library: [libpthread.so.0]
0x0000000000000001 (NEEDED) Shared library: [libdl.so.2]
0x0000000000000001 (NEEDED) Shared library: [libutil.so.1]
0x0000000000000001 (NEEDED) Shared library: [librt.so.1]
0x0000000000000001 (NEEDED) Shared library: [libm.so.6]
0x0000000000000001 (NEEDED) Shared library: [libc.so.6]
0x000000000000000c (INIT) 0x41b700
0x000000000000000d (FINI) 0x569238
0x000000006ffffef5 (GNU_HASH) 0x400298
0x0000000000000005 (STRTAB) 0x40f6a8
0x0000000000000006 (SYMTAB) 0x4035b8
0x000000000000000a (STRSZ) 36637 (bytes)
0x000000000000000b (SYMENT) 24 (bytes)
0x0000000000000015 (DEBUG) 0x0
0x0000000000000003 (PLTGOT) 0x7f9fe8
0x0000000000000002 (PLTRELSZ) 8040 (bytes)
0x0000000000000014 (PLTREL) RELA

最佳答案

至少有3种方法可以设置动态链接器搜索路径:

  1. 导出 LD_LIBRARY_PATH=/home/user/new_lblas
  2. 重写二进制文件,以将 DT_RPATH 动态标记设置为 /home/user/new_lblas
  3. 重写二进制文件,以将 DT_RUNPATH 动态标记设置为 /home/user/new_lblas

上面的#1和#2是等价的,但是#3(这就是你正在做的)不是。区别很微妙:RPATH 影响二进制文件所有共享库,但 RUNPATH 仅影响二进制文件,并且递归地应用于此二进制文件加载的共享库。

这解释了为什么 LD_LIBRARY_PATH 有效,但 RUNPATH 无效。

查看 patchelf man page ,看来您想要:

--force-rpath
Forces the use of the obsolete DT_RPATH in the file instead of
DT_RUNPATH. By default DT_RPATH is converted to DT_RUNPATH.

关于python - 为 python 设置 RPATH 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43616505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com