Static Linking Considered Harmful

There are still too many people out there who think (or even insist) that static linking has benefits. This has never been the case and never will be the case. Here are a few reasons why dynamic linking is superior:

There are certainly more reasons. The often used argument about statically linked apps being more portable (i.e., can be copied to other systems and simply used since there are no dependencies) is not true since every non-trivial program needs dynamic linking at least for one of the reasons mentioned above. And dynamic linking kills the portability of statically linked apps.

Conclusion: Never use static linking!


There is one aspect where people decide to misinterpret these recommendations. I do not say that everything should be stuffed into its own DSO. I would never say this. To the contrary. Code which belongs together because of the development process should be kept in the same object. The reason is that the fewer DSOs are needed the faster everything works (loading, symbol resolution, ...).

One example where this still isn't done right is If you look at the dependencies of the binaries (swriter.bin etc) you'll see lots of files from the project. Almost all of them are used in all the programs and none is used outside the project. Especially there is which itself pulls in a huge number of DSOs. This is done in all programs!

During deployment there is no reason to do this. All the DSOs pulled in by and probably some of the others used in the various programs should all be part of one mega-DSO. Yes, this DSO would be big, but it would be smaller than sum of all the loaded dependencies. And more: the export lists can be further restricted making a lot of calls inside the new DSO much faster.

There is a good reason to have all the separate DSOs during development. This makes it possible to rebuild only a small part of the source tree to test out some changes. But this is development. When the binaries are shipped to users (e.g., as part of a distribution) this need falls away and so does the justification.So, combine all the sources from the same project which are used or at least loaded in all or most situations in one single DSO. It will be smaller and faster. Just don't get overzealous and add 3rd party code as well. E.g., even if you ship a copy of, say, libxml don't add it to your DSO. Only the code which is written as part of the project.