less_retarded_wiki/library.md
Miloslav Ciz af3e90bf66 Update
2022-05-01 16:02:11 +02:00

22 lines
3.3 KiB
Markdown

# Library
Software library is code that's not meant to run on its own but rather be used by other programs. A library provides resources such as [functions](function.md), [macros](macro.md), [classes](class.md) or [constants](constant.md) that are normally related to solving some specific class of problems, so e.g. there are [GUI](gui.md) libraries, [audio](audio.md) libraries, [mathematical](math.md) libraries etc. Libraries exist to prevent [reinventing wheels](reinventing_wheel.md) by only ever implementing the code once so that next time we can simply reuse it (respecting the [DRY](dry.md) principle). Examples of libraries are the [standard C library](clib.md), [SDL](sdl.md) or [JQuery](jquery.md).
If a programmer wants to use a specific library, he has to first [install](install.md) it (if it's not installed already) and then *include* it in his program with a specific command (words like `include`, `using` or `import` are commonly used). Then he is able to use the resources the library exports. Depending on the type of the library he may also need to [link](linking.md) the library code after [compilation](compiler.md) and possibly distribute the library files along with his program.
You will often hear a library as a certain [API](api.md) -- this is the interface of the library consisting of the elements via which programmer uses the library, mostly the [functions](function.md) the library offers. If a programmer wants to know the library API, he wants to know the names of the functions, what [parameters](parameter.md) they take etc. Sometimes there may be multiple libraries with the same API but different internal implementations, this is nice because these libraries can be easily [drop-in-replaced](drop_in.md).
In a specific [programming language](programming_language.md) it IS generally possible to use a library written in a different language, though it may be more difficult to achieve.
We generally divide libraries to two types:
- **[static](static.md)**: The library code is embedded into the executable of the final program so that the library files have to be distributed along with the program. This is more convenient and also makes sure the program uses exactly the correct version of the library. But of course this results in bigger executable, and if we have multiple programs that use the same library which is statically linked, each program will have a [redundant](redundancy.md) copy of the library code, wasting memory (both storage and [RAM](ram.md)).
- **[dynamic](dynamic.md)** (also *shared*): The compiled library code resides in a separate file ([DLL](dll.md) on [Windows](windows.md), [.so](so.md) in [GNU](gnu.md)/[Linux](linux.md)) which may need to be distributed along with the program, but this one file can be shared among all programs that use the library so the compiled programs can be smaller. It may also be easier to update the library to a new version by simply replacing the compiled library file. RAM may also be saved as the dynamic library may be loaded just once for multiple simultaneously running programs.
Many times a library can have both static and dynamic version available, or the compiler may allow to automatically link the library as static or dynamic. Then it's up to the programmer which way he wants to go.
## C Libraries
## LRS Libraries
TODO