What is a library?
A library is a package of code that is meant to be reused
by many programs. Typically C functions/C++ classes and methods which
can be shared by more than one application are broken out of the application's
source code, compiled and bundled into a library.
Why use libraries?
- Libraries rarely change, they do not need to be recompiled often. It would be a waste of time to recompile the library every time you wrote a program that used them.
- Each and every object file need not be stated when linking because the developer can reference the individual library. This simplifies the multiple use and sharing of software components between applications.
- Allows application vendors a way to simply release an API to interface with an application.
- Components which are large can be created for dynamic use, thus the library remain separate from the executable reducing it's size and thus disk space used.
- Because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important to businesses or people who don’t want to make their source code available for intellectual property reasons.
What library contains?
Typically, a C++ library comes
in two pieces:
1) A header file that defines
the functionality the library is exposing (offering) to the programs using it.
2) A precompiled binary that contains the implementation of that functionality pre-compiled into machine language.
2) A precompiled binary that contains the implementation of that functionality pre-compiled into machine language.
Some libraries may be split
into multiple files and/or have multiple header files.
Types of Libraries
There are two library types:
1)Static
Library(Archive)
In windows : .lib extension
In Linux : .a(archive) extension
2)Dynamic
Library(Shared Library)
In windows : .dll (Dyamic Link
Library) extension
In Linux : .so(Shared Object) extension
Static Library
|
Dynamic Library
|
Consists of
routines that are compiled and linked directly into your program.
|
Consists of
routines that are loaded into your application at run time.
|
All the
functionality of the static library becomes part of your executable.
|
The library
does not become part of your executable — it remains as a separate unit.
|
Copy of the
library becomes part of every executable that uses it, this can cause a lot
of wasted space.
|
Many
programs can share one copy, which saves space.
|
Static
libraries also can not be upgraded easy — to update the library, the entire
executable needs to be replaced.
|
Can be
upgraded to a newer version without replacing all of the executables that use
it.
|
Always
loaded and whatever version of the code you compiled with is the version of
the code that will run.
|
Dynamic
libraries are stored and versioned separately.
|
Static
binaries will load and run faster due to the fact that there is no need for
extra indirection to access symbols.
|
Dynamic libraries are not linked into your
program, programs using dynamic libraries must explicitly load and interface
with the dynamic library.
|
References: Static and dynamic libraries