Thread: Understanding libraries, dynamic, static, shared and import

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    107

    Understanding libraries, dynamic, static, shared and import

    In c::b you can create dynamic, static and shared libraries. Firstly, I thought a dynamic library was a shared library so why is there two different options in c::b (A.1 — Static and dynamic libraries « Learn C++).


    So, on compiling a dynamic dll, say myDll you get, myDll.dll, libmyDll.a and libmyDll.def. Why do we need the libmyDll.a, I thought this extension was a static library, and also why do we need the myDll.def. These are the definitions of the functions in the dll i know, is this needed when using loadlibrary(), I don't ever use a def file so what's point of it?. Can you not use load library without a .def, or when is the .def actually needed? Is this the same for the MSVC compiler?


    Now when compiled, a static library gives, dllmyStatic.a. That makes sense to me, you can link and use the functiers etc in your program and the whole library is integrated into you program regardless of the functions you are using.


    Now a compiled shared library seems to give the same as a dynamic dll, libMyshared.dll, libMyshared.def and libMyshared.a.


    I understand how to use a function from a dynamic library using loadlibrary(). I understand how to use a static library by linking it in the IDE and using the headers and then the functions are available to be called. I don't see the difference between a dynamic dll and shared dll in c::b though. Nor do I see the need for 3 files, .dll, .a and .def to be produced from both dynamic and shared libraries. Any tips here?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Short description:
    - MyLibrary.a - the code for the library. In case of static libraries, this is all the code. In case of dynamic libraries, it contains a stub that calls the functions inside the external, so you can just link as with a static library and call the functions anyway without LoadLibrary.
    - MyLibrary.dll - contains the actual code in case of a dynamic library. Multiple programs can share the same DLL, but the DLL is loaded only once into memory.
    - MyLibrary.def - contains a description of what files are public (i.e. callable) from outside the DLL. Used at compile time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The meanings vary a bit between host systems, but in rough terms

    1) A dynamic library is loaded dynamically by the program, either when it starts up (during program startup) or explicitly in code (e.g. under windows using LoadLibrary()). This means the library is a distinct file from the executable. A .def file is used by the linker, but not if the library is loaded by code.

    2) A static library is used as an input to the linker. The linker ensures that a copy of all functions needed by a program are included in the executable. The executable can then run without a separate library being shipped with it.

    3) A shared library is sometimes just another name for a dynamic library. However, on some systems, if multiple programs use the same library, the host system ensures that only one copy of the library code is in memory, rather than having one copy of the library for every executing program. [Note: shared libraries can also be used to share data between the programs, but that's optional - the default is that there is one copy of code (managed by the OS) in memory, and each program gets its own data (variables, etc)].
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Shared libraries
    By Aslaville in forum C++ Programming
    Replies: 3
    Last Post: 01-26-2013, 07:14 AM
  2. GCC: Compiling with both static and shared libraries
    By eatwithaspork in forum C Programming
    Replies: 4
    Last Post: 06-23-2008, 01:48 PM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Shared Libraries and such
    By divineleft in forum Linux Programming
    Replies: 3
    Last Post: 11-04-2006, 08:38 PM
  5. Dynamic import
    By knutso in forum Windows Programming
    Replies: 2
    Last Post: 05-20-2005, 03:12 AM