Difference between static and shared libraries?
Solution 1
Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files. All the code relating to the library is in this file, and it is referenced by programs using it at run-time. A program using a shared library only makes reference to the code that it uses in the shared library.
Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one].
There are advantages and disadvantages in each method:
Shared libraries reduce the amount of code that is duplicated in each program that makes use of the library, keeping the binaries small. It also allows you to replace the shared object with one that is functionally equivalent, but may have added performance benefits without needing to recompile the program that makes use of it. Shared libraries will, however have a small additional cost for the execution of the functions as well as a run-time loading cost as all the symbols in the library need to be connected to the things they use. Additionally, shared libraries can be loaded into an application at run-time, which is the general mechanism for implementing binary plug-in systems.
Static libraries increase the overall size of the binary, but it means that you don't need to carry along a copy of the library that is being used. As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there.
Personally, I prefer shared libraries, but use static libraries when needing to ensure that the binary does not have many external dependencies that may be difficult to meet, such as specific versions of the C++ standard library or specific versions of the Boost C++ library.
Solution 2
A static library is like a bookstore, and a shared library is like... a library. With the former, you get your own copy of the book/function to take home; with the latter you and everyone else go to the library to use the same book/function. So anyone who wants to use the (shared) library needs to know where it is, because you have to "go get" the book/function. With a static library, the book/function is yours to own, and you keep it within your home/program, and once you have it you don't care where or when you got it.
Solution 3
Simplified:
- Static linking: one large executable
- Dynamic linking: a small executable plus one or more library files (.dll files on Windows, .so on Linux, or .dylib on macOS)
Solution 4
Static libraries are compiled as part of an application, whereas shared libraries are not. When you distribute an application that depends on shared libaries, the libraries, eg. dll's on MS Windows need to be installed.
The advantage of static libraries is that there are no dependencies required for the user running the application - e.g. they don't have to upgrade their DLL of whatever. The disadvantage is that your application is larger in size because you are shipping it with all the libraries it needs.
As well as leading to smaller applications, shared libraries offer the user the ability to use their own, perhaps better version of the libraries rather than relying on one that's part of the application
Solution 5
The most significant advantage of shared libraries is that there is only one copy of code loaded in memory, no matter how many processes are using the library. For static libraries each process gets its own copy of the code. This can lead to significant memory wastage.
OTOH, a advantage of static libraries is that everything is bundled into your application. So you don't have to worry that the client will have the right library (and version) available on their system.
Mohit Deshpande
I am a researcher at The Ohio State University in the field of computer vision and machine learning. I have worked as a mobile apps instructor for Zenva and current work as a writer for Zenva in machine learning.
Updated on July 08, 2022Comments
-
Mohit Deshpande almost 2 years
What is the difference between static and shared libraries?
I use Eclipse and there are several project types including Static Libraries and Shared Libraries? Does one have an advantage over the other?
-
JustJeff about 14 yearsexecutable image is bigger on disk, as well as in memory, when using static libs.
-
Jasmeet about 14 yearsThats correct, that is what I was alluding to when I said everything is bundled into your application.
-
gheese over 10 yearsDLL hell as it has been known
-
Tony Delroy about 10 years"replace the shared object with ... functionally equivalent, but may [improve] performance": specifically, equivalent caller-facing functionality in semantic use of the API (application programming interface: function signatures and variables including types), but implementation-side functionality may differ in more than perf.: e.g. function always logs to file -> also log to TCP server:port expected in $MY_APP_LOG_SERVER.
-
Tony Delroy about 10 years"[.sos incur a] small additional cost for execution of the functions" - that's possible (if function groups/ordering have been optimised for cache locality in the static link, or due to oddities in OS/loader/compiler/architecture like cross-segment/large-pointer perf. penalties), but on many architectures/compiler-settings the dynamic linker patches the call to create the exact same calling machine opcodes.
-
Tony Delroy about 10 years"As the code is connected at compile time there are not any additional run-time loading costs. The code is simply there." - yes and no... it's all in the executable image ready to be paged in if execution requires, but - starting from a situation where your program hasn't run recently enough to be in cache - with shared libraries it's possible (sometimes likely or certain) that the OS, a driver or another running program will have already loaded the same shared library your app wants to use, in which case it may be in cache and your program start and run faster.
-
jduncanator almost 10 yearsWhat some people have failed to mention is that with static libraries the compiler knows which functions your application needs and can then optimize it by only including those functions. This can cut down on library size massively, especially if you only use a really small subset of a really large library!
-
ElefEnt over 7 yearsThis answer could be better organized. It would be helpful to make bullet lists for pros/cons or a table to show the differences in each dimension where there is a difference.
-
463035818_is_not_a_number almost 7 years"Static libraries are compiled as part of an application" ... static libraries are compiled as static libraries and linked as part of an application
-
simplename over 5 yearsCould you elaborate on "but use static libraries when needing to ensure that the binary does not have many external dependencies"? Why is static linking preferred in that case?
-
Anya Shenanigans over 5 yearsWhen you compile dynamically, you are declaring a dependency that is resolved at run-time. Meeting this dependency requires either (a) carrying a copy of the library with the program, or (b) ensuring the library is installed on the target system before running. This means the deployment of the program becomes more complicated. Static linking puts all these dependencies in the program at compile time, thus, typically, reducing the deployment to a single file.
-
dmckee --- ex-moderator kitten over 5 years"It also allows you to replace the shared object with one that [better]" Or--as the happy pendants pointed out in the comments to my answer to a similar question)--with one that is worse as well. I.e. installing the updated version of a library that contains a new bug means that your program gets the bug just as much as installing a bug-fixed version of the library solves that bug for you. Win some, lose some. Probably more win than lose with well supported libraries.
-
Soner from The Ottoman Empire about 5 yearsAdditionally,
.so
files on *nix systems are kinda shared(dynamic) library. -
bloody about 4 yearsAnother shortcoming of a static library is that the developer has no control over the visibility of symbols.