为什么一个程序可以运行找不到动态库?

Why a program can run with a dynamic library not found?

安装SAP HANA成功后,可以看到sapstartsrv进程运行:

# ps -ef | grep sapstartsrv
xn1adm     2143      1  0 Mar29 ?        00:00:05 /usr/sap/XN1/HDB00/exe/sapstartsrv pf=/home/hana/shared/XN1/profile/XN1_HDB00_dhe2 -D -u xn1adm

但是在使用ldd命令检查sapstartsrv动态链接库后:

# ldd /usr/sap/XN1/HDB00/exe/sapstartsrv
    linux-vdso.so.1 (0x00007ffd35a30000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007fd1fe6b3000)
    librt.so.1 => /lib64/librt.so.1 (0x00007fd1fe4ab000)
    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fd1fe270000)
    libpam.so.0 => /lib64/libpam.so.0 (0x00007fd1fe061000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007fd1fde5e000)
    libuuid.so.1 => /usr/lib64/libuuid.so.1 (0x00007fd1fdc59000)
    libsapnwrfccm.so => not found
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fd1fd8d7000)
    libm.so.6 => /lib64/libm.so.6 (0x00007fd1fd5d6000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fd1fd3bf000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fd1fd1a2000)
    libc.so.6 => /lib64/libc.so.6 (0x00007fd1fcdfb000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd1fe8b7000)
    libaudit.so.1 => /usr/lib64/libaudit.so.1 (0x00007fd1fcbd8000)

我可以看到 libsapnwrfccm.so 没有找到。但是为什么sapstartsrv程序可以运行找不到动态库?

因为某些东西通过将链接器指向包含该库的目录来启动该程序。通常这是通过设置环境变量 LD_LIBRARY_PATH 完成的。例如:

LD_LIBRARY_PATH=/look/here/for/libs program

运行时链接器现在还会在 /look/here/for/libs 中查找 program 所依赖的库。