二进制找不到特定库,但可以在同一目录中找到其他库

二进制找不到特定库,但可以在同一目录中找到其他库

我有一个可以使用 clang 或 gcc 编译的二进制文件,应用程序的目录如下所示:

/app root dir
  /bin
    /<application>
  /lib
    /<application specific libs>
  /syslib
    /<system libs, e.g. boost libraries>

该二进制文件使用(除其他外)两个编译器的以下选项进行编译:

-Wl,-rpath,../syslib -ljansson -lsqlite3 -lboost_date_time -lboost_filesystem -lboost_log -lboost_program_options -lboost_thread -lboost_system

因此,在执行过程中,应用程序应该能够找到提供的系统库。但是,当我使用用 g++ 编译的二进制文件时,它无法找到特定的库。 syslib 目录中可用的库有:

 - libboost_date_time.so -> libboost_date_time.so.1.65.1
 - libboost_date_time.so.1.65.1 libboost_filesystem.so ->
 - libboost_filesystem.so.1.65.1 libboost_filesystem.so.1.65.1
 - libboost_log.so -> libboost_log.so.1.65.1 libboost_log.so.1.65.1
 - libboost_program_options.so -> libboost_program_options.so.1.65.1
 - libboost_program_options.so.1.65.1 libboost_system.so ->
 - libboost_system.so.1.65.1 libboost_system.so.1.65.1
 - libboost_thread.so -> libboost_thread.so.1.65.1
 - libboost_thread.so.1.65.1 libjansson.so -> libjansson.so.4.10.0
 - libjansson.so.4 -> libjansson.so.4.10.0 libjansson.so.4.10.0
 - libsqlite3.so -> libsqlite3.so.0.8.6 libsqlite3.so.0 ->
 - libsqlite3.so.0.8.6 libsqlite3.so.0.8.6 libstdc++.so ->
 - libstdc++.so.6.0.24 libstdc++.so.6 -> libstdc++.so.6.0.24
 - libstdc++.so.6.0.24

当我执行 g++ 编译的二进制文件时,它找不到 libboost_thread 库,而使用 clang++ 编译的二进制文件时,它工作正常。二进制错误输出:

./DGM: error while loading shared libraries: libboost_thread.so.1.65.1: cannot open shared object file: No such file or directory

g++ 编译的二进制文件的 ldd 输出:

linux-vdso.so.1 =>  (0x00007fff7c90c000)
libjansson.so.4 => ../syslib/libjansson.so.4 (0x00007fea61f7b000)
libboost_date_time.so.1.65.1 => ../syslib/libboost_date_time.so.1.65.1 (0x00007fea61d69000)
libboost_filesystem.so.1.65.1 => ../syslib/libboost_filesystem.so.1.65.1 (0x00007fea61b4d000)
libboost_log.so.1.65.1 => ../syslib/libboost_log.so.1.65.1 (0x00007fea6186c000)
libboost_program_options.so.1.65.1 => ../syslib/libboost_program_options.so.1.65.1 (0x00007fea615e9000)
libboost_system.so.1.65.1 => ../syslib/libboost_system.so.1.65.1 (0x00007fea613e3000)
libstdc++.so.6 => ../syslib/libstdc++.so.6 (0x00007fea6105d000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fea60d51000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fea60b39000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fea6091b000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fea60556000)
libnetsnmpagent.so.30 => /usr/lib/x86_64-linux-gnu/libnetsnmpagent.so.30 (0x00007fea602ec000)
libnetsnmp.so.30 => /usr/lib/x86_64-linux-gnu/libnetsnmp.so.30 (0x00007fea60012000)
libsqlite3.so.0 => ../syslib/libsqlite3.so.0 (0x00007fea5fd09000)
--> libboost_thread.so.1.65.1 => not found <--
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fea5fb01000)
/lib64/ld-linux-x86-64.so.2 (0x00007fea639f3000)
libwrap.so.0 => /lib/x86_64-linux-gnu/libwrap.so.0 (0x00007fea5f8f6000)
libperl.so.5.18 => /usr/lib/libperl.so.5.18 (0x00007fea5f56d000)
libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007fea5f193000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fea5ef8e000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fea5ed74000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fea5eb3b000)

clang++ 编译的二进制文件的 ldd 输出:

linux-vdso.so.1 =>  (0x00007fff0cdc5000)
libjansson.so.4 => ../syslib/libjansson.so.4 (0x00007fca078f9000)
libsqlite3.so.0 => ../syslib/libsqlite3.so.0 (0x00007fca075f1000)
libboost_date_time.so.1.65.1 => ../syslib/libboost_date_time.so.1.65.1 (0x00007fca073de000)
libboost_filesystem.so.1.65.1 => ../syslib/libboost_filesystem.so.1.65.1 (0x00007fca071c3000)
libboost_log.so.1.65.1 => ../syslib/libboost_log.so.1.65.1 (0x00007fca06ee2000)
libboost_program_options.so.1.65.1 => ../syslib/libboost_program_options.so.1.65.1 (0x00007fca06c5e000)
--> libboost_thread.so.1.65.1 => ../syslib/libboost_thread.so.1.65.1 (0x00007fca06a38000) <--
libboost_system.so.1.65.1 => ../syslib/libboost_system.so.1.65.1 (0x00007fca06832000)
libstdc++.so.6 => ../syslib/libstdc++.so.6 (0x00007fca064ac000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fca061a0000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fca05f89000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fca05d6a000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fca059a5000)
libnetsnmpagent.so.30 => /usr/lib/x86_64-linux-gnu/libnetsnmpagent.so.30 (0x00007fca0573b000)
libnetsnmphelpers.so.30 => /usr/lib/x86_64-linux-gnu/libnetsnmphelpers.so.30 (0x00007fca05539000)
libnetsnmp.so.30 => /usr/lib/x86_64-linux-gnu/libnetsnmp.so.30 (0x00007fca0525f000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fca0505a000)
librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fca04e52000)
/lib64/ld-linux-x86-64.so.2 (0x00007fca08a94000)
libwrap.so.0 => /lib/x86_64-linux-gnu/libwrap.so.0 (0x00007fca04c47000)
libperl.so.5.18 => /usr/lib/libperl.so.5.18 (0x00007fca048be000)
libcrypto.so.1.0.0 => /lib/x86_64-linux-gnu/libcrypto.so.1.0.0 (0x00007fca044e4000)
libnsl.so.1 => /lib/x86_64-linux-gnu/libnsl.so.1 (0x00007fca042c9000)
libcrypt.so.1 => /lib/x86_64-linux-gnu/libcrypt.so.1 (0x00007fca04090000)

我发现这非常奇怪,因为 g++ 编译的二进制文件可以在同一目录中找到其他 boost 库。

根据要求,libboost_thread.so 的 g++ 编译二进制文件的 strace 输出与 libboost_program_options.so 进行了比较:

libboost_program_options.so:

getcwd("/opt/dap/apps/DGM/bin", 128)    = 22
mmap(NULL, 3016424, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0a5558d000
mprotect(0x7f0a55666000, 2097152, PROT_NONE) = 0
mmap(0x7f0a55866000, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0xd9000) = 0x7f0a55866000
close(3)                                = 0
open("../lib/tls/x86_64/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../lib/tls/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../lib/x86_64/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../lib/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../syslib/tls/x86_64/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../syslib/tls/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../syslib/x86_64/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
open("../syslib/libboost_program_options.so.1.65.1", O_RDONLY|O_CLOEXEC) = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\300\233\2\0\0\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=667552, ...}) = 0

libboost_thread.so:

getcwd("/opt/dap/apps/DGM/bin", 128)    = 22
mmap(NULL, 3175320, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x7f0a53a34000
mprotect(0x7f0a53b36000, 2097152, PROT_NONE) = 0
mmap(0x7f0a53d36000, 20480, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x102000) = 0x7f0a53d36000
mmap(0x7f0a53d3b000, 920, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x7f0a53d3b000
close(3)                                = 0
access("/etc/ld.so.nohwcap", F_OK)      = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/tls/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/x86_64-linux-gnu/tls/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/tls/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/x86_64-linux-gnu/tls", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/x86_64-linux-gnu/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/lib/x86_64-linux-gnu/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/x86_64-linux-gnu", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
open("/usr/lib/x86_64-linux-gnu/tls/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/x86_64-linux-gnu/tls/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/tls/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/x86_64-linux-gnu/tls", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/x86_64-linux-gnu/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64-linux-gnu/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/x86_64-linux-gnu", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
open("/lib/tls/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/tls/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/lib/tls/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/tls", 0x7fffc0976a60)        = -1 ENOENT (No such file or directory)
open("/lib/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib/x86_64", 0x7fffc0976a60)     = -1 ENOENT (No such file or directory)
open("/lib/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/usr/lib/tls/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/tls/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/usr/lib/tls/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/tls", 0x7fffc0976a60)    = -1 ENOENT (No such file or directory)
open("/usr/lib/x86_64/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib/x86_64", 0x7fffc0976a60) = -1 ENOENT (No such file or directory)
open("/usr/lib/libboost_thread.so.1.65.1", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
stat("/usr/lib", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
writev(2, [{"./DGM", 5}, {": ", 2}, {"error while loading shared libra"..., 36}, {": ", 2}, {"libboost_thread.so.1.65.1", 25}, {": ", 2}, {"cannot open shared object file", 30}, {": ", 2}, {"No such file or directory", 25}, {"\n", 1}], 10./DGM: error while loading shared libraries: libboost_thread.so.1.65.1: cannot open shared object file: No such file or directory
) = 130
exit_group(127)                         = ?
+++ exited with 127 +++

相关内容