How is a "static library" different from a "DLL"

J

jr

I am working in VC++ so this may be microsoft specific though I don't think
so. Excuse me if it is.

Assuming it isn't, can someone generally explain the purpose of having
both - is it purely a deployment/distribution thing.

I notice that the static library generates obj files and a Lib file. Could
someone kindly explain how they relate to one another.

Thanks very much

John
 
X

Xenos

jr said:
I am working in VC++ so this may be microsoft specific though I don't think
so. Excuse me if it is.

Assuming it isn't, can someone generally explain the purpose of having
both - is it purely a deployment/distribution thing.

I notice that the static library generates obj files and a Lib file. Could
someone kindly explain how they relate to one another.

Thanks very much

John

--

An obj file is generated when a C++ source file (et. al.) is compiled. It
is linked with other (or possibly none) obj files to form an executable. A
lib file is an archive (equilivant to a unix *.a file). It contains 1 or
more objs. Your linker will search a lib file for undefined symbols. If it
finds any symbols that it needs in the archive, it will link in the
object(s) that contain them. A DLL is a dynamically linked library (similar
to a unix shared library or *.so file). It is loaded during runtime by
programs that need it and is shared by other running programs that also need
it.

Simplicistic, but good enough I guess.
 
J

jr

Thanks
Great help.

Xenos said:
An obj file is generated when a C++ source file (et. al.) is compiled. It
is linked with other (or possibly none) obj files to form an executable. A
lib file is an archive (equilivant to a unix *.a file). It contains 1 or
more objs. Your linker will search a lib file for undefined symbols. If it
finds any symbols that it needs in the archive, it will link in the
object(s) that contain them. A DLL is a dynamically linked library (similar
to a unix shared library or *.so file). It is loaded during runtime by
programs that need it and is shared by other running programs that also need
it.

Simplicistic, but good enough I guess.
 
D

Dave Townsend

John,

DLL = dynamic link library. This means that the library code is not
actually
linked into your executable, but remains in a separate file and is loaded
either
when you start the executable, or can actually be loaded on-request by the
program.

DLLs are somewhere between the .lib file and the executable on the
evolutionary path,
DLLs are actually linked , .lib files are merely repositories for .obj files
and other .libs, just
packaged into a more convenient medium. Symbols in DLLs have to be
partially resolved,
that is to say if you call foo() it better be in the DLL or in another DLL
which the first
DLL refers to.

..lib files are linked into the executable code. The code in the .lib file
"becomes" part of
the executable. They are really just collections of .obj's

When you create a DLL, a .lib is also created. This is actually a "proxy"
which I will explain.

The DLL's .lib file will contain stubs or dummies for each of the functions
you export from the dll.
The .lib is linked into your executable as a place holder to call functions
in the DLL.
When you make a call to a function foobar(), the .lib stub foobar() will
actually execute the
function in the dll for you. This is all taken care of under the hood for
you when you use DLLs.


The advantages of DLLs are:

1. you can update the DLL without relinking the executable or
redistribution the executable.
( provided you don't add new functions or changes interfaces )
2. One DLL can be shared amongst a number of different executables so you
save on disk storage.
In reality, there are normally several copies of a DLL because
executables often require an
exact version of a dll to work correctly.
3. Since DLLs can be loaded on-demand, the start up time of the exectuable,
its memory footprint, etc
can be minimized to the essential functionality. Many of microsoft's
products make heavy use of
this feature through another technology called COM, which provides a
streamlined means to
execute functions in a dynamically loaded DLL.

Disadvantages:
1. DLL HELL - you have multiple versions of DLL which are all incompatible
with each other.
2. DLL code may be slightly slower in execution speed because of the code
has be to
relocatable at run-time and certain optimizations cannot be performed.
3. You have to ship DLLS along with the executable, so its a bit more
complex in deployment.
You also have to be sure your executable is picking up the correct
versions of DLLs on the
system.
4. There are some arcane rules for exporting functions from a DLL, just
adding the code is
not enough. However, these rules are fairly mechanical, and can be
mastered after a few
encounters.

Hope that helps.

dave
 
D

David White

Dave Townsend said:
DLL = dynamic link library. This means that the library code is not
actually
linked into your executable, but remains in a separate file and is loaded
either
when you start the executable, or can actually be loaded on-request by the
program.

[snip]

It should be added that the OP's suspicion that his question might be
MS-specific, or at least platform-specific, was correct. None of this stuff
has anything to do with the C++ language.

DW
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,458
Latest member
Chris#

Latest Threads

Top