0%

指定运行时动态库位置

指定运行时动态库路径

  • gcc参数指定 -Wl,-rpath=${LD_PATH}
  • 设置环境变量 export LD_LIBRARY_PATH=${LD_PATH}
  • 配置文件 /etc/ld.so.conf

背景

编译 mosquitto 得到几个可执行文件,运行时出现错误

$ ./mosquitto_sub --help
./mosquitto_sub: error while loading shared libraries: libmosquitto.so.1: cannot open shared object file: No such file or directory
$ ldd mosquitto_sub
    linux-vdso.so.1 (0x00007fffe178b000)
    libmosquitto.so.1 => not found
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fc8c9ca5000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fc8ca2a3000)

找不到 libmosquitto.so.1, 需要指定路径

GCC参数

-Wl,-rpath=${LD_PATH}

环境变量

$ export LD_LIBRARY_PATH=../lib
$ ldd mosquitto_sub
    linux-vdso.so.1 (0x00007ffd3410b000)
    libmosquitto.so.1 => ../lib/libmosquitto.so.1 (0x00007f4ecc2d5000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4ecbee4000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f4ecbcdc000)
    libssl.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libssl.so.1.0.0 (0x00007f4ecba74000)
    libcrypto.so.1.0.0 => /usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007f4ecb631000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4ecb412000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f4ecc6fb000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f4ecb20e000)

只在当前 shell 有效

配置文件

修改 /etc/ld.so.conf 文件以添加私有路径