Thread: Static library vs Dynamic library

  1. #1
    C Newbie
    Join Date
    Aug 2010
    Posts
    29

    Static library vs Dynamic library

    Can someone tell me the difference of a Static C library, and a dynamic library, and when to use each one of them.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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).

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Quote Originally Posted by bithub View Post
    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.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C#] Intercept SysListView32 item added
    By Devils Child in forum Windows Programming
    Replies: 9
    Last Post: 03-26-2010, 07:29 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. dynamic or static binding
    By noob2c in forum C++ Programming
    Replies: 1
    Last Post: 08-06-2003, 01:43 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 09:38 AM