What is a library?

A library contains code which has been written by other programmers.

It contains useful routines (functions) that you can use in your own program.

Using libraries saves considerable time. You have probably used the library System.IO, which contains all the functions to handle files (i.e. retreiving and saving data with the StreamReader and StreamWriter classes).

Object Oriented libraries often contain classes.

text

Here are several libraries which you have relied on to build your programs.

Why use libraries?

  • Using libraries saves time because you can rely on the expertise of others. You don't have to learn the skills or take the time to write the code yourself.
  • Libraries seperate code so that the complexity is hidden away in a seperate place.
  • Libraries are fully tested by their developers but also when used by other programmers. This makes debugging easier as you can assume that the library code works.

How do libraries work?

.LIB and .DLL files

During compilation (when you build your program) any specified libraries will be linked to the main program. Libraries can either be static: built in with your code, or dynamic: used when you run your program at the point where they are needed.

text

LIB is a static library where functions and procedures can be placed and called as the application is being compiled. A DLL or Dynamic Link Library does the same function but is dynamic in a sense that the application can call these libraries during run-time and not during the compilation.

text

Are there any disadvantages to using a library?

  • Static libraries can significantly increase the size of your compiled file as each library contains many functions, many of which you probably don't need.
  • To use a library you need to understand someone else's often expert code or at least how to use the API (public functions that you can use).
  • You have to trust that the library has been properly written and tested.

Linkers

A linker is special program used during compilation to combine object files, generated by the compiler (or assembler) and other pieces of code to create an executable file.

Loaders

The loader loads the executable file into memory at run time and allocates memory for the program.

Geeks4Geeks