Can someone tell me the difference of a Static C library, and a dynamic library, and when to use each one of them.
Can someone tell me the difference of a Static C library, and a dynamic library, and when to use each one of them.
Static libraries get their code compiled into the executable. This means that your executable is bigger, but you do not need the library during runtime. Dynamic libraries do not get compiled into the executable, instead they are loaded during runtime. This means the dynamic library must exist on the system you are running the executable on.
bit∙hub [bit-huhb] n. A source and destination for information.
What bithub said, plus to make note that some functions have to be in Dynamic Linked Libraries, if they are used by multiple executables or, in Windows at least, if you are setting system hooks. The rest of the time static linking is just fine and many times a fair bit faster.
Important points of comparison:
Static binaries will load and run faster due to the fact that there is no need for extra indirection to access symbols.
Shared libraries will take up less disk space and memory. Disk space because static libraries include the used parts of the library in each binary, and memory because one of the huge selling points of shared libraries is that each library is loaded into memory once, and all programs that need to use it use the same copy.
If you remove or break a shared library, anything that depends on it will have problems. This includes ABI changes. Statically linked programs will continue to run.
But, if there is a bug in a library, and it is shared, you can replace it once and all affected programs will be fixed. You have to relink each static program or the faulty code will remain (anybody remember when a security bug was found in zlib way back when, and a lot of programs included it statically?)
If you need to distribute a binary, you might do well to link it statically, or else you'll have to require the user to have the proper shared libraries. For programs on your own system (at least on Unix-like systems), I'd recommend shared libraries because of the memory savings and ability to upgrade in place. The only thing I'd use statically linked programs for is for rescue (say if libc gets broken).
Everything cas said is correct, except that while static binaries load faster, I don't believe they run faster. Once the code is linked and loaded into memory, it should run at the same speed.
bit∙hub [bit-huhb] n. A source and destination for information.
Shared libraries do not know the addresses at which their symbols will be mapped (indeed, they will differ for various programs that link them). As such, they make use of position-independent code, which can be mapped to any address, but at a small cost. When statically linking, relocatable objects are used, which can be mapped properly at link time, incurring no cost.
It's entirely possible that some clever advancements have been made that reduce this impact; I haven't bothered looking at comparisons recently, because I really have no use for static linking; even if it were faster, I'll take the advantages of dynamic linking.