Why use libraries in C?
When writing code, we often use libraries to help us fill in the gaps when we don’t feel like reinventing the wheel… Which is great! Let’s not reinvent things just for the sake of working hard. That’s DRY on a global scale! So we know why libraries are good, but do we know how they do their goodness?
No? Let me help you!
(This is for Linux systems, things may differ across systems)
The First thing: Static & Dynamic Libraries
Just in case you need a refresher: Libraries are just files with a bunch of functions in them.
So the first thing you should know is the difference between a static library and a dynamic (shared) library. They’re both useful for different things.
Static libraries are better for quickly getting a library up and running, but it also has drawbacks. It gets compiled into every program that uses it. This means you have to manually update every program that uses it when you edit your library. Not fun!
Dynamic libraries are nice because they load only 1 copy of themselves in memory when you run a program, so you don’t eat up as much memory when you start running multiple programs using that library. The other benefit is that your programs don’t need to be recompiled when you recompile your shared library.
Still interested? Nice! Now let’s make one.
Making a Dynamic Library
In this article I’ll go over dynamic libraries. I made a nice article on static libraries that explains how to make and use them.
First things first: we need to get all of our functions we want to use in one place. However you do this, just make sure to compile those functions as object files, and use the flag -fpic or -fPIC to let the compiler know you’ll be turning it into a dynamic library.
It should look roughly like this:
gcc -c -fPIC -Werror *.c
- -Werror just throws you an error in case there’s something you should fix in your functions.
- *.c should be whatever .c files you want included in your library.
- -c turns all the files into object files. They have the same name but end in .o instead of .c
If everything compiles and the object files look good, then we can turn them into a library!
The command for this should look roughly like so:
gcc -shared -o libmyfunctions.so *.o
- -shared tells the compiler to make a shared (dynamic) library
- o tells it to make the output file libmyfunctions.so.
- Whatever your name is, make sure to prepend it with lib, which will let the compiler know it’s looking at a library file. Append it with .so, which is the shared library output file type.
- *.o should be whatever output files you made from the previous command
Congratulations, you have a new library!
How does your Shell Program REALLY work?
For us programmers, the command line is often taken for granted. We type in commands, run programs, change files…
But how does all of this happen?
You probably use a CLI like terminal or PuTTY, but your CLI doesn’t know what you’re typing — it’s the shell program running INSIDE the CLI which interprets what you type.
I wrote a post about this earlier this year, but after learning a lot of C, I thought I should write a more in-depth post on this question.
I recently made a custom shell, so I had to figure out how all of…
How to create static libraries
To create a static library you have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .
Creating a Static Library step by step:
Step 1.
Create all your source files. Source files hold any functions you will use.
Step 2.
Compile the source files into object files. Using GCC use this command:
$ gcc -c *.c
This will change the object files to source files.
Step 3.
Create a static library. Using “libholberton” as an example of a library name, this command creates a Static library.
$ ar -rc libholberton.a *.o
The ‘ar’ is the program being used to archive the files. The ‘c’ tells the program to create a library. The ‘r’ tells the program the replace or update older files in the library.
With step three a static library has been created.
If needed use the ‘ranlib <libraryname.a> to index the library.
$ ranlib libholberton.a
Indexing a library will let the compiler know just how old a library file is. Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols. This step may or may not be necessary depending on your computer system.
To view the contents of the static library access it using: ‘ar -t libholberton.a’ in the command line.
The ‘nm’ command will let you see the symbols in your library; nm lib_test.a
.
To use the static library call on the library during the linking phase of program compilation.
gcc main.c -L. -lholberton -o main
How to use them?
Now our static library “libname.a” is ready to be used. we can use it in a program. This is done by adding the library’s name to the object file(s) given to the linker. First, let us create a C source file that uses the above created static library.
Now we can use the command below to create our final executable program:
$ gcc main.c -L. -lname -o main
This will create a program using the object file “main.o”, and any symbols it requires from the “name” static library.
Flags description:
-L : Specifies the path to the given libraries (‘.’ referring to the current directory)
-l : Specifies the library name without the “lib” prefix and the “.a” suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.
All we have to do now is to run our program.
That was my summary of static libraries!
Hopefully, you found this useful. In the next blog post, we will cover dynamic libraries and the difference between them and static libraries.