less_retarded_wiki

main page, file list (580), source, all in md+txt+html+pdf, report abuse, stats, random article, consoomer version

Library

Software library (often shortened to just lib) is program code that's not meant to run on its own but rather to be used by other programs, i.e. it is a helpful collection of preprogrammed code that's meant to be reused. A library provides resources such as functions, macros, classes or constants that are normally related to solving some specific class of problems, so e.g. there are GUI libraries, audio libraries, mathematical libraries etc. Libraries exist mostly to prevent reinventing wheels by only ever implementing the code once so that next time we can simply reuse it (respecting the DRY principle), but they also e.g. help assure others are using an already well tested code, they help to implement modularity etc. Examples of libraries are the standard C library, SDL or JQuery. Libraries are not to be confused with frameworks which are larger, more bloated environments.

In Unix environments there is a convention for library packages to start with the lib prefix, so e.g. SDL library is named libsdl etc.

Standard library (stdlib) is a term that stands for the set of libraries that officially come with given programming language -- these libraries usually offer very basic functionality (such as I/O and basic math) and are required to always be present on every system.

If a programmer wants to use a specific library, he usually has to first somehow install it (if it's not installed already, usually a library is some kind of software package) 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 of the library. Depending on the type of the library he may also need to link the library code after compilation and possibly distribute the library files along with his program. A more KISS approach is for a library to simply be a code that's somehow copy-paste included in the main program (see single header libraries etc.).

As a programmer you will encounter the term library API -- this is the interface of the library consisting of the elements via which programmer uses the library, mostly the functions the library offers. API is what the programmer interacts with; the rest is library internals (its implementation) that's usually supposed to not be touched and stay a bit hidden (see encapsulation). If a programmer wants to know the library API, he wants to know the names of the functions, what parameters 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. The library API is usually part of its documentation -- when learning a new library X, you want to search the internet for something like X library API reference to see what functions it offers.

In a specific programming language it IS generally possible to use a library written in a different language, though it may be more difficult to achieve -- see language bindings and wrappers.

We generally divide libraries to two types:

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

TODO: example

Header only or single header library is a kind of keep-it-simple library that's wholly implemented in a single header (.h) file -- this is kind of a hack going against "official recommendations" as header files aren't supposed to contain implementation code, just declarations, however single header libraries are suckless/LRS, convenient and very easy to use, as they don't have to be linked and are nicely self-contained, distributed in one nice file. A traditional library would consist of one or more header (.h) files and one or more implementation (.c) files; such library has to be compiled on its own and then linked to the program that uses it -- the idea behind this was to compile the library only once and so save time on recompiling it again and again; however this justification is invalid if our library is simple enough and compiles very quickly (which it always should, otherwise we are dealing with badly designed bloat). A single header library can therefore just be included and just works, without any extra hassle -- yes, its code recompiles every time the program is compiled, but as stated, it doesn't hurt if our library is well designed and therefore simple. Single header libraries often include the option (via some #define macro) to include just the declaration or both declarations and implementation code -- this is useful if our main program is composed of multiple source files and needs to be linked. LRS libraries, such as small3dlib or raycastlib, are of course single header libraries.

LRS Libraries

TODO


Powered by nothing. All content available under CC0 1.0 (public domain). Send comments and corrections to drummyfish at disroot dot org.