如果我尝试链接到一个未知的库(-lunknown
)并查看(使用strace)来查看链接器正在做什么,我注意到,在stat
返回-1后立即(ENOENT
),链接器尝试open
它刚刚发现的文件不存在。从strace输出中,我们看到:
stat("/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/libunknown.so", 0x7fff6694df50) = -1 ENOENT (No such file or directory)
open("/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/libunknown.so", O_RDONLY) = -1 ENOENT (No such file or directory)
stat("/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/libunknown.a", 0x7fff6694dfb0) = -1 ENOENT (No such file or directory)
open("/usr/bin/../lib/gcc/x86_64-linux-gnu/4.7/libunknown.a", O_RDONLY) = -1 ENOENT (No such file or directory)
... [lots of similar lines here]
ld版本:
$ ld -v
GNU ld (GNU Binutils for Ubuntu) 2.23.2
为什么链接器会在open
返回-1后尝试stat
?