Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it completely safe to remove unused libraries as reported by ldd?

I'm cleaning up a rotted source tree, and I try to make each executable and each shared library to link only with the libraries they use directly.

In order to do that, I ldd -u- r the binary output, and remove the reported libraries from the makefile.

For example:

$ ldd -u -r ./libA.so
Unused direct dependencies:
        /usr/local/lib/libB.so
        /usr/local/lib/libC.so
        /lib/tls/libpthread.so.0
$ sed -i'' -e 's/-lB//' -e 's/-lC//' Makefile

Well, of course libpthread is really needed (and is anyway implicitly included with -pthread), but the rest of the libraries reported by ldd can be safely remove.

Are there implications to my optimization? Is it completely safe?

like image 935
Elazar Leibovich Avatar asked Oct 30 '25 07:10

Elazar Leibovich


1 Answers

It is safe*, unless one thing happens (which is, thankfully, under your control).

If the application loads additional symbols with dlsym(RTLD_DEFAULT,...), it will search currently loaded shared libraries for the appropriate symbol (function). If its intent is to load the symbol from one of these libs you want to remove (and there was no prior dlopen() call), the application won't find it, and may misbehave.

Note, however, dlsym() is used very seldom (and you may check if it's used via ldd), and it's even more seldom to use it without a prior call to dlopen(), so in nearly all cases you may safely remove unused libraries. It will significantly improve the portability of your application.


*by "safe" I mean "likely to work if there were no build-time errors". Build-time ramifications of removing libraries are too easy to detect to account for them.

like image 186
P Shved Avatar answered Nov 01 '25 12:11

P Shved



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!