Question

Probably on any OS it is possible to compile C++/C standard library statically or dynamically. On Windows I prefer static builds always, because it helps to avoid "dll hell" problem with different versions of libraries installed or not installed on specific Windows version, edition and service pack, etc. Static linking makes software more portable and less dependent on what end user did with his operating system (I even saw examples when end user could make SHIFT+DEL on some DLLs in system32, he couldn't explain why, or when users claim that my app contains virus because it tried to download dynamically linked prerequisites from official Microsoft website...) So, on Windows static linking is usually better than dynamic one in my experience. However, I am new to Linux, so can anyone share his experience? My question is: what kind of linking (dynamic or static) is preffered on Linux if we ignore the fact that dynamic one allows to save memory & hard drive space and if we plan to distribute software with automated install program (hard drive space and memory are cheap enough now, so there are not reason to sacrifice hours of working time required to create really good and portable installer to win some megabytes of RAM or hard drive space). Are there any Linux-specific issues with dynamic/static linking?

Was it helpful?

Solution

On Linux you normally have a package manager that ensures you only have one version of libraries installed. So there normally is no dll hell and no problem with linking dynamically. Linking dynamically is the standard way on Linux.

OTHER TIPS

I'd say the answer depends on how you distribute the software.

If you package the software for a specific Linux distribution and version dynamic linking is usually preferred. You know which libraries to find on the system and you can specify dependencies.

However, if you want to distribute the software as a Linux binary that runs on "any" system (such as various games or software like Matlab for example) you will end up with the same dll (or .so) hell problem as on windows. You don't know which versions of which libraries are on the system. Thus, you will have to provide your own .so files or link statically.

See the whole point in using dynamic linking is to reduce the size of executables and memory usage.If you neglect that there is too less to talk about.

On the other hand you mentioned about saving memory and disk space.It is necessary to save disk space because when you want to export your app/program, you can't put a 2Gb app on the internet for download(for example openCV library is about 2.1GB). The solution is to dynamically link them and load only those modules which are necessary to you.This enables efficient multitasking also(creates just one copy of the module and the whole program uses the same copy). peculiarly:

For example, a media player application might originally be shipped with a codec that supports the mp3 file format. If the media player were statically linked it would not be possible to dynamically update it to support a different file format, without replacing the entire application. Dynamic linking means that a new version of the shared library containing a more up-to-date codec, which includes some enhancements and bug fixes, could be dynamically loaded by a dynamic linker into memory at run-time to replace the original shared library. A shared library can also be shared by more than one application. For example, two different media players could both use the same shared library containing the same codec. This potentially means that the device running the application requires less physical memory, depending on the size of the dynamic linker.

third, in linux everything is dynamically linked except for the /bin/ash.static whic also has its dynamic version /bin/ash but this shouldn't stop you from static linking in linux. when using gcc the linking is by default dynamic.I guess you should use the "-static" flag to statically link the libraries

@Vitaliy good that you brought this up.The important thing to note here is that Smart linking and the creation of shared (or dynamic) libraries are mutually exclusive, that is, if you turn on smart linking, then the creation of shared libraries is turned of.

smart linking breaks the code into small code blocks and their dependencies are loaded. So if you are calling a dependency multiple times, it gets loaded multiple times. This gives a very good execution time but very high compilation time especially for large units.So there is a certain trade-off.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top