Static Libraries in C: What, Why, How, and When?Josef GoodyearBlockedUnblockFollowFollowingMar 3Libraries in programming are just like normal libraries, minus the dust!What is a Library?Libraries in computer programming are very similar to libraries in the real world.
Essentially, they are an organizational tool.
Where physical libraries store books, programming libraries store functions.
In C and other languages, a library is simply a way to hold a bunch of commonly used functions together in one place.
Libraries are useful because they allow us to use functions without explicitly linking each program to every single function it uses.
With that in mind, you might want to include specific functions in a library if you you are constantly using them in programs.
One more thing to note: A library can’t be executed or run at all.
It’s just a file that the compiler can refer to.
Static Libraries and Shared LibrariesThere are two main types of libraries: static and shared.
Static libraries have a .
a or a .
lib extension, depending on the operating system.
Static libraries are inserted into a program during the linking phase of compilation.
Once the executable is created, the library isn’t needed for it to run.
The benefits of using a static library might be very apparent.
Imagine you are making a friend to run on another computer.
If you used a static library, that program is completely self-contained.
He or she won’t need to worry about having the right libraries for the program to work because everything is already included in the program!.Of course, this comes with tradeoffs.
For one, using static libraries is much more memory intensive than using a shared library.
Shared libraries (also known as dynamic libraries) have a .
dll, or a .
dylib extension, depending on the operating system.
Shared libraries are never actually included in the executable.
During compilation, the compiler checks the library for functions, but doesn’t insert them into the program.
Instead, the executable accesses the library during runtime.
Therefore, if a program utilizes a shared library, it needs to have access to that library whenever the program is run.
Obviously, portability takes a hit when considering shared libraries, because, you can’t just run the program by itself; you need the libraries too.
However, if you have several programs which share the same library, we can run them much more efficiently using a dynamic library.
How to Create a Static LibraryWe now have the basics of what different types of libraries are, and when you might use one or the other.
Now, let’s say your situation prompted you to opt for a static library.
How would you go about making one?ar rc staticlib.
oYou might type something like the above example.
ar is a shell command, which can create, modify, and extract from archives.
Next to the command, we see two flags, modifying that ar.
The first, is r.
r will update and replace a function if there is a newer version of it.
c, the next flag, will create the library if it doesn’t exist yet.
Next, we see the name of the library, staticlib.
The next 3 entries are the object files we want to include in our library.
Of course, we can utilize wildcards here, if we’d like, in order to generalize what kind of files we want to include.
After we create our archive, we want to index it.
This makes the linking process more efficient, by keeping a record of what’s included in the library.
aRunning ranlib on the library will index it.
Finally, if you want to see what your library contains, you can run the nm command, which lists symbols from object files.
An example of what nm might returnHow to Use a Static LibraryWe’ve learned how to create a static library, but that’s not going to be helpful to us if we don’t know how to use one of them!.Since we’re working with a static library, we need to include our library in compiling process.
-lstaticlib -o programnameWe compile our C file using some important flags: L and l.
L tells the compiler to look for library files in the current directory.
As long as staticlib.
a is in the current directory, it will be included in the process.
Next, l marks our library file, telling the compiler that staticlib.
a is, in fact, a library.
Notice that we don’t include the “.
a” at the end of the file’s name.
Finally, we link the c file and our library into “programname”, which we can then execute, without the need for the library to be present.