【问题标题】:Using `--as-needed` but `ldd -u -r` still reports unused direct dependencies使用 `--as-needed` 但 `ldd -u -r` 仍然报告未使用的直接依赖项
【发布时间】:2019-07-19 19:32:56
【问题描述】:

对于我的一个二进制文件,CMake 会生成如下所示的命令行:

/opt/rh/devtoolset-8/root/usr/bin/g++  
-O3 -DNDEBUG   
-s 
-Wl,--as-needed 
-Wl,--gc-sections 
<blah-blah>.o
...
-o procmon.e 
-Wl,-rpath,/usr/local/lib64
<my lib>.a
...
/usr/local/lib64/libxalan-c.so 
/usr/local/lib64/libxerces-c.so
/home/user/vcpkg/installed/x64-linux/lib/libcurl.a 
...
-lcrypt 
<more vcpkg static libs>
-lrt 
-lpthread 
<more vcpkg static libs>
-lm 
<more vcpkg static libs>
-ldl 
<more vcpkg static libs>
-pthread 
<more vcpkg static libs>

如您所见,--as-needed 已指定,但我仍然得到未使用的依赖项:

$ ldd -u -r procmon.e
Unused direct dependencies:
        /usr/local/lib64/libxalan-c.so.111
        /lib64/libcrypt.so.1
        /lib64/libm.so.6

为什么?

【问题讨论】:

    标签: gcc linker ld ldd


    【解决方案1】:

    --as-needed 在链接阶段通过忽略未解析任何当前未知符号的共享库(当前正在处理)来工作。垃圾收集部分 (--gc-sections) 在此之后启动,它可能会导致删除(在上一步中)使用给定共享库解析的所有符号引用,从而导致原始帖子中提到的行为。

    Here is 一些额外的阅读。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-02-10
      • 2018-11-02
      • 1970-01-01
      • 2021-08-08
      • 1970-01-01
      相关资源
      最近更新 更多