💾 Archived View for yujiri.xyz › software › guide › libraries.gmi captured on 2024-07-09 at 00:07:26. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2024-03-21)
-=-=-=-=-=-=-
Learn programming for good instead of profit
A library is a bunch of code that's expected to be useful as part of many different programs, and so it's developed separately from any specific program. Programs usually depend on a few libraries. For example, there's a library called libpng ('lib' stands for library) which contains code for dealing with PNG images. Almost any program that displays or manipulates PNG images probably uses libpng.
Also, each programming language has one library called the *standard library* of that language (abbreviated 'stdlib'), which contains code considered so essential that it's installed along with the language's compiler or interpreter itself. A standard library typically contains code for doing things like making syscalls and dealing with common data structures and file formats.
There are two different ways programs can use libraries: static linking and dynamic linking.
In static linking, the program and the library are compiled together, producing just one executable file that contains machine code for both the program itself and whatever parts of the library it needs. In theory, a statically linked executable doesn't need any other files present to run: you can copy just that one file onto a computer and the program will work. (In practice, there are many caveats to this even with static linking, such as different processor architectures, different operating systems, or depending on things other than libraries, like resource files and configuration files.)
In dynamic linking, the library is compiled separately, into something called a 'shared object' file (on Unix) or DLL (dynamically loaded library, on Windows), and installed into a separate folder on your computer. Then the program is compiled, without the library, into a dynamically linked executable that contains information about what libraries it needs. When the operating system runs a dynamically linked executable, it reads that information, finds the installed libraries, and links them together at run-time. (If it can't find the libraries, the program won't work.)
One reason dynamic linking is popular is to save space. If you have a library that (like libpng) is gonna be used by a dozen different programs, they can all share one copy of libpng. If all the programs were statically linked, they'd all have their own copies of libpng, embedded inside their executables. In practice it's more complex than that because most programs only use part of the library, and by static linking they include only the parts they use; while with dynamic linking you have to have the entire library installed even if all your programs only use part of its functionality.
Another and perhaps more compelling reason to use dynamic linking is that it means you can update a library just once and all your programs will be using the updated version. If your programs were statically linked, you would need to update all the programs that use it in order to use the new version of the library.
The downside of dynamic linking is that there are many more strings attached to the program, and thus more things can go wrong with installing it. If you're using your Linux distribution's package manager, it'll theoretically handle all these strings for you, but that's a lot of extra work for the people who maintain the distribution, and sometimes they make a mistake, causing a situation where two packages can't be installed at the same time because they require different versions of some library. If you're not using your distribution's package manager, you'll have to hunt down and install all the required libraries yourself, which is a massive pain. Even if they're already installed, they might be an incompatible version or located in the wrong places.
A binding is a type of library. Its purpose is to "wrap" a library written in C so it can be used from another language. For example, SDL is a popular graphics library used for game programming, and PySDL is a Python binding for SDL, which allows you to write games in Python using SDL.
Most high-level languages have ways to directly use code written in C (which is how the binding works on the inside), but doing so is difficult as it requires writing some code that can't take advantage of the features of the high-level language. The point of a binding is to do the hard part so that actually using the underlying library is easier.