Thread: difference between dynamic link library and static library

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    difference between dynamic link library and static library

    Hello

    I got a real basic question. Whacan you guys/gals tell me what is the difference between dynamic link library and static library?

    from my understanding the dynamic link library gets linked at runtime (when a function in the dll is called ??) and the static library is compiled along with the code. is that correct?

    i came across it some place that a dll can be dynamically linked as well as statically linked ???

    what does it mean to being linked at runtime?
    Last edited by studentc; 01-11-2006 at 05:27 AM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by studentc
    i came across it some place that a dll can be dynamically linked as well as statically linked ???
    what does it mean to being linked at runtime?
    No Idea what that might mean. A dll can only be linked dynamically.
    I know of 2 different ways to use a dll.
    One way is to use it via an importlibrary. -> your program links against the import lib and that library handles the actual loading of the dll.
    Another way is to load the dll directly with LoadLibrary() and find the functions with GetProcAddress().
    Kurt

  3. #3
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Quote Originally Posted by ZuK
    A dll can only be linked dynamically.
    Are you sure? The msdn homepage is saying a different thing
    Please check here .

    Afrinux

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    What I understand from this link is that you can create different types of dll's when using MFC. MFC itself exists in two versions, as a static library or a dll.
    The difference is how MFC is linked to your dll ( statically or dynamically ). The Outcome is still a dll that can only be linked dynamically to another application.
    Kurt

  5. #5
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    well, with linux you can create a static library, that is compiled into your executable file, the problem is that it greatly increases the size of the exectutable itself.

    a dynamic link library is a library that is available for all applications that want to use it, and is loaded into the stack when first called on. If you start a second application that calls for it as well, instead of loading a second instance of it into the ram, the loader simply links to the one in ram, and supplies pointers to the memory address block containing it to the application.

    a shared library like this requires the position independant code flag during the compiling of it, or it won't be usable. [ gcc it's -fpic ] I know the Sams Teach Yourself Linux Programming book has an excellent explanation of the differences, and where you might find a static library to be usefull, as well as a diagram showing how the shared library works in the stack.
    Last edited by Jaqui; 01-11-2006 at 02:05 PM.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Are you sure? The msdn homepage is saying a different thing
    No it doesnt. DLL stands for Dynamic Linked Library. Calling a statically linked library a DLL would be a complete misnomer.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    70
    thanks for the replies

    if two applications link to the same shared dll, is there not a possibility of the dll getting corrupted?

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> if two applications link to the same shared dll, is there not a possibility of the dll getting corrupted?

    Certainly no such risk.

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by studentc
    thanks for the replies

    if two applications link to the same shared dll, is there not a possibility of the dll getting corrupted?
    No, because on any sensible operating system, the running applications are not capable of writing to the memory that the shared library lives.

  10. #10
    Registered User
    Join Date
    May 2004
    Posts
    70
    so the shared library is read only?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    No, because on any sensible operating system, the running applications are not capable of writing to the memory that the shared library lives.
    What? Of course a running application can write to the memory where the linked library is. They share the same process space.

    The reason the dll doesn't get corrupted is that when a new application loads a dll, the dll is loaded in that application's process space. The memory of a dll is not shared among the different processes UNLESS you explicity declare it that way with special compiler directives.

  12. #12
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Sorry, I have no familiarity with Windows libraries whatsoever. I responded to the question "if two applicants link to the same shared dll, is there not a possibility of the dll getting corrupted?" because it specifically contained the word "shared".

    Under unix systems, a shared library is implemented such that 1000 different processes can all use the same shared library, but only one copy exists physically in memory. I thought that the whole point of shared libraries was so that you didn't need one copy of a library for each application.

    I didn't realise this would be any different in Windows. I've now looked up and read a few references about windows dlls and I see that there is an ambiguity in the meaning of "shared library"

  13. #13
    Registered User Jaqui's Avatar
    Join Date
    Feb 2005
    Posts
    416
    Here is an image that shows why a "shared" library doesn't get corrupted and why they use fewer reasources than a static library.
    Quote Originally Posted by Jeff Henager
    If the average user can put a CD in and boot the system and follow the prompts, he can install and use Linux. If he can't do that simple task, he doesn't need to be around technology.

  14. #14
    Registered User Afrinux's Avatar
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    46
    Thank you ZuK and bithub for clearing up things for me.

  15. #15
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    So my statement of "the running applications are not capable of writing to the memory that the shared library lives" was actually correct? I'm confused now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  2. C++ runtime, dynamic link, mingw.
    By 39ster in forum C++ Programming
    Replies: 1
    Last Post: 12-16-2007, 12:00 PM
  3. using VC6 link VC7 static lib error
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2006, 10:58 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM