Ubuntu下使用Intel OneAPI时修改LD_LIBRARY_PATH无效的问题
问题背景
在Ubuntu 20系统中使用Intel Fortran编译器编译依赖自行编译HDF5库的代码,已将HDF5库路径添加至LD_LIBRARY_PATH,执行echo $LD_LIBRARY_PATH可见路径已包含,但运行编译后的程序时仍报错找不到指定库。
相关验证信息
- LD_LIBRARY_PATH输出:
$ echo $LD_LIBRARY_PATH /opt/intel/oneapi/tbb/2021.9.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/rkcommon/1.11.0/lib:/opt/intel/oneapi/ospray_studio/0.12.0/lib:/opt/intel/oneapi/ospray/2.11.0/lib:/opt/intel/oneapi/openvkl/1.3.2/lib:/opt/intel/oneapi/oidn/1.4.3/lib:/opt/intel/oneapi/mpi/2021.9.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.9.0//lib/release:/opt/intel/oneapi/mpi/2021.9.0//lib:/opt/intel/oneapi/mkl/2023.1.0/lib/intel64:/opt/intel/oneapi/itac/2021.9.0/slib:/opt/intel/oneapi/ispc/1.19.0/lib:/opt/intel/oneapi/ipp/2021.8.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.7.0/lib/intel64:/opt/intel/oneapi/ipp/2021.8.0/lib/intel64:/opt/intel/oneapi/embree/4.0.1/lib:/opt/intel/oneapi/dnnl/2023.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/debugger/2023.1.0/gdb/intel64/lib:/opt/intel/oneapi/debugger/2023.1.0/libipt/intel64/lib:/opt/intel/oneapi/debugger/2023.1.0/dep/lib:/opt/intel/oneapi/dal/2023.1.0/lib/intel64:/opt/intel/oneapi/compiler/2023.1.0/linux/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/x64:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/oclfpga/host/linux64/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/ccl/2021.9.0/lib/cpu_gpu_dpcpp:“:/home/samz/local/lib/phdf5/1.8.23/build_ifort_2021_impi_2021/lib”
- 运行报错信息:
./****.exe: error while loading shared libraries: libhdf5_fortran.so.10: cannot open shared object file: No such file or directory
- HDF5库目录验证:目标库确实存在于指定路径:
$ ls -la /home/samz/local/lib/phdf5/1.8.23/build_ifort_2021_impi_2021/lib total 11420 drwxrwxr-x 2 samz samz 4096 Apr 10 14:35 . drwxrwxr-x 6 samz samz 4096 Apr 10 14:35 .. -rw-r--r-- 1 samz samz 6763194 Apr 10 14:35 libhdf5.a -rw-r--r-- 1 samz samz 419748 Apr 10 14:35 libhdf5_fortran.a -rwxr-xr-x 1 samz samz 1115 Apr 10 14:35 libhdf5_fortran.la lrwxrwxrwx 1 samz samz 25 Apr 10 14:35 libhdf5_fortran.so -> libhdf5_fortran.so.10.0.8 lrwxrwxrwx 1 samz samz 25 Apr 10 14:35 libhdf5_fortran.so.10 -> libhdf5_fortran.so.10.0.8 -rwxr-xr-x 1 samz samz 341456 Apr 10 14:35 libhdf5_fortran.so.10.0.8 -rw-r--r-- 1 samz samz 229896 Apr 10 14:35 libhdf5_hl.a lrwxrwxrwx 1 samz samz 19 Apr 10 14:35 libhdf5_hl_fortran.a -> libhdf5hl_fortran.a -rw-r--r-- 1 samz samz 127576 Apr 10 14:35 libhdf5hl_fortran.a -rwxr-xr-x 1 samz samz 1292 Apr 10 14:35 libhdf5hl_fortran.la lrwxrwxrwx 1 samz samz 20 Apr 10 14:35 libhdf5_hl_fortran.so -> libhdf5hl_fortran.so lrwxrwxrwx 1 samz samz 27 Apr 10 14:35 libhdf5hl_fortran.so -> libhdf5hl_fortran.so.10.0.7 lrwxrwxrwx 1 samz samz 27 Apr 10 14:35 libhdf5hl_fortran.so.10 -> libhdf5hl_fortran.so.10.0.7 -rwxr-xr-x 1 samz samz 106024 Apr 10 14:35 libhdf5hl_fortran.so.10.0.7 -rwxr-xr-x 1 samz samz 1080 Apr 10 14:35 libhdf5_hl.la lrwxrwxrwx 1 samz samz 20 Apr 10 14:35 libhdf5_hl.so -> libhdf5_hl.so.10.2.4 lrwxrwxrwx 1 samz samz 20 Apr 10 14:35 libhdf5_hl.so.10 -> libhdf5_hl.so.10.2.4 -rwxr-xr-x 1 samz samz 163408 Apr 10 14:35 libhdf5_hl.so.10.2.4 -rwxr-xr-x 1 samz samz 983 Apr 10 14:35 libhdf5.la -rw-r--r-- 1 samz samz 6699 Apr 10 14:35 libhdf5.settings lrwxrwxrwx 1 samz samz 17 Apr 10 14:35 libhdf5.so -> libhdf5.so.10.4.1 lrwxrwxrwx 1 samz samz 17 Apr 10 14:35 libhdf5.so.10 -> libhdf5.so.10.4.1 -rwxr-xr-x 1 samz samz 3491568 Apr 10 14:35 libhdf5.so.10.4.1
- LD_DEBUG调试结果:动态链接器的搜索路径中未包含HDF5的有效路径,片段如下:
... 126670: search path=/opt/intel/oneapi/tbb/2021.9.0/env/../lib/intel64/gcc4.8:/opt/intel/oneapi/rkcommon/1.11.0/lib:/opt/intel/oneapi/ospray_studio/0.12.0/lib:/opt/intel/oneapi/ospray/2.11.0/lib:/opt/intel/oneapi/openvkl/1.3.2/lib:/opt/intel/oneapi/oidn/1.4.3/lib:/opt/intel/oneapi/mpi/2021.9.0//libfabric/lib:/opt/intel/oneapi/mpi/2021.9.0//lib/release:/opt/intel/oneapi/mpi/2021.9.0//lib:/opt/intel/oneapi/mkl/2023.1.0/lib/intel64:/opt/intel/oneapi/itac/2021.9.0/slib:/opt/intel/oneapi/ispc/1.19.0/lib:/opt/intel/oneapi/ipp/2021.8.0/lib/intel64:/opt/intel/oneapi/ippcp/2021.7.0/lib/intel64:/opt/intel/oneapi/embree/4.0.1/lib:/opt/intel/oneapi/dnnl/2023.1.0/cpu_dpcpp_gpu_dpcpp/lib:/opt/intel/oneapi/debugger/2023.1.0/gdb/intel64/lib:/opt/intel/oneapi/debugger/2023.1.0/libipt/intel64/lib:/opt/intel/oneapi/debugger/2023.1.0/dep/lib:/opt/intel/oneapi/dal/2023.1.0/lib/intel64:/opt/intel/oneapi/compiler/2023.1.0/linux/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/x64:/opt/intel/oneapi/compiler/2023.1.0/linux/lib/oclfpga/host/linux64/lib:/opt/intel/oneapi/compiler/2023.1.0/linux/compiler/lib/intel64_lin:/opt/intel/oneapi/ccl/2021.9.0/lib/cpu_gpu_dpcpp:“/tls/haswell/x86_64:“/tls/haswell:“/tls/x86_64:“/tls:“/haswell/x86_64:“/haswell:“/x86_64:“ (LD_LIBRARY_PATH) ...
临时解决方法
通过在动态链接器默认搜索的目录中创建HDF5库的软链接,暂时解决了库找不到的问题,但希望明确LD_LIBRARY_PATH无效的根本原因。
注:环境通过.bashrc中的. /opt/intel/oneapi/setvars.sh > /dev/null加载Intel OneAPI环境。
根本原因分析
关键问题点:你的LD_LIBRARY_PATH中HDF5路径被错误地包裹在了中文双引号“”中,并且路径前还有一个多余的":前缀,导致动态链接器将整个带引号的字符串识别为一个无效路径,而非实际的HDF5库目录。从LD_DEBUG的输出也能看到,链接器搜索的路径里出现了“/tls/haswell/x86_64这类异常内容,说明引号破坏了路径的分隔逻辑。
这种错误通常是在.bashrc或其他配置文件中添加LD_LIBRARY_PATH时,错误地使用了中文引号,或者拼接路径时格式错误导致的。
修复方法
打开
.bashrc文件,找到添加HDF5路径的语句,确保格式正确:export LD_LIBRARY_PATH=/home/samz/local/lib/phdf5/1.8.23/build_ifort_2021_impi_2021/lib:$LD_LIBRARY_PATH注意:使用英文半角引号(如果需要的话),或者直接不使用引号,路径拼接时不要出现多余的符号。
确保该语句在加载Intel OneAPI的
setvars.sh之后执行,避免被Intel的环境变量覆盖。重新加载
.bashrc生效:source ~/.bashrc验证
LD_LIBRARY_PATH中的HDF5路径已正确显示,无多余引号和符号:echo $LD_LIBRARY_PATH
内容的提问来源于stack exchange,提问作者Sam Z

