编译时去除未使用的依赖库

如下, --no-as-needed 会把所有指定的库标记为 NEEDED, 即使没用到; --as-needed 会忽略没用到的库;

$ cc -Wl,--no-as-needed xx.c -ldl -lz -lm -l pthread
$ ldd a.out
        linux-vdso.so.1 (0x00007fffc03d1000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fad234ae000)
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fad23491000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fad2334d000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fad2332b000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fad23156000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fad234c5000)
$ cc -Wl,--as-needed xx.c -ldl -lz -lm -l pthread
$ ldd a.out
        linux-vdso.so.1 (0x00007ffeb7df8000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f88d1ec3000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f88d20a9000)
$
posted @ 2023-06-15 17:42  sinpo828  阅读(116)  评论(0编辑  收藏  举报