C Librum Programitus
A Blog posted for the Holberton Curriculum

If you’re like me, you have an incredible love for breaking it down and breaking it often. This can mean you need a box for your screws or filing storage for smaller pieces. In C, this is your library. You use your library to tell your functions what variables are passable.
You’ve got a couple of library styles from which to choose. You can use a static library and bring your tools with your project everywhere you go or you can grab a dynamic library and just reach for tools as you need them.
A static library will copy itself into the header of your function during the run process. This can take extra space in your functions and really bog down a system. These files are truly portable. You can dragon drop that file into your cloud and work it anywhere. If you’re trying to make a series of functions that can run independently, a static library is the choice for sure.
- A pretty big reason you might want to utilize a static library is that your file is executable and you want to share it. Loan your buddy the anchor he needs and not the whole boat as it were.
A dynamic library is a direct access library that doesn’t copy into your function. If you go with a dynamic library, you’re going to have to go fetch your tools one at a time. That can mean a much more energy efficient workspace, but a proprietary one. It’s all up to what you’re trying to achieve.
Some reasons you’d want to utilize a dynamic library are:
- Changing your library after compilation wouldn’t require going back to the original source files and individually making changes to the library origin, then recompiling it. This can mean you can make massive changes to a minimalist system with fewer steps. This can also mean that nothing in your system is independently portable.
- The sheer space save of not making 10,000 copies of your library.
- A company can make all their machine readable code far more private this way.

So, let’s take a quick glance at the process involved in the creation of a library.
When you compile a library, you use object files. Object files are functions that have been compiled up to the linking stage. You’ll see these files ending in .o
The function you’ll use in gcc to compile like this would look something like

gcc -c filename.c otherfile.c thatfile.c thisfile.c filefile.c
Running the above function will produce
filename.o otherfile.o thatfile.o thisfile.o filefile.o
Object files. Not linked, but machine readable and ready to collect for the right librarian!
Now it is time to make a library.
Libraries make me feel quite a bit piratey. Insert joke here about sailing the seven C’s. Your functions for library creation are shorthanded “archive” actions. For a static library, it’s actually as simple as “ar”(sadly not ahoy or avast. Ye landlubber). The flags you’d use to create a static library are quite important.
ar -rc libcombatwombat.a *.o
would create a static library named “libcombatwombat” whose flags indicate it is to be [c]reated [r]eplacing any previous copies of the files named. And the asterisk usage indicates that you intend to utilize all .o files in the current directory. While this is an incredibly simple example, the man page for ar will cover the rest. I’m just here to talk. I personally find unix.com very easy to read. Here, have a hot link. Make sure you drink some water after. They’re a little spicy.
https://www.unix.com/man-page/v7/1/ar/
Not trying to learn how to sock? Then you’re making a dynamic library. No more static here. If you’re compiling a dynamic library, you’re looking at script more like
gcc *.c -c -fPIC
With those flags, you’re telling the compiler to generate an object file for all the .c files in the working directory, same as before. the -fPIC flag sequence is so that your files remain position independent. This will allow the processors or operating systems that utilize them to decide where to handle them during run time.
gcc *.0 -shared -o libwombatfortress.so
All your .o files are belong to the “libwombatfortress” library. This is in part the resolution of the above script. The -shared flag is exactly as dangerous as it sounds. It’s a dynamic flag. Not dangerous. It does what it says.
It is always in your best interest to name your libraries beginning with lib. It’s one of the places where convention is a beautiful process.
When you’re reading an archive of archives trying to find a specific lot, it’ll make your life easier to not just keep seeing marsupial names and bad puns. Learn from my mistakes. . . and lunacy.
So, the main benefit of a library is allowing yourself access to all of these functions you’ve created and allowing them to be utilized in a categorical fashion.
Why would you want to make a library? You don’t want to have to go recode all of the information you’ve already had to write once.
I’ve always firmly believed in the importance of hard work to justify my future laziness. This is a fairly exacting routine generated by the use of libraries.
If you want bad jokes and a layman knowledge of the basics of programming, stay tuned.