Static Libraries, What Are They?

John Cook
4 min readMar 4, 2019

What is a library, in C it is a collection of functions that are predefined by the user or someone else, that can be used in a program to perform a specific task. For example the isdigit function from the ctype.h library will tell if a character is a number or not. This is useful for math, lets say the goal is to add two numbers but the user makes a typo and instead of 2+2 types W+2, if used properly isdigit will send an error message that W is not a number and to try again. There are many such functions that are very useful when writing a complicated program, and these functions are stored in various libraries.

I will focus on static libraries in this article, but there are also dynamic libraries that have their purpose. Why use static libraries, specifically the standard libraries? First of all its generally easier then writing custom functions. Second these functions are very bug free and will cover 99% of cases they are designed for, and the few bugs that exist are well documented so they can easily be avoided. Third they are way faster then custom code most of the time. They have been used by thousands of users who have sent in feedback and tweaks to speed up the code, it is highly unlikely that custom code will be faster because these functions are so well defined. Finally they are portable, the standard library will run on almost any machine, this also allows for better readability of code instead of looking up what a custom function does an experienced user will know what a standard library function is supposed to do.

However often custom code is needed, and needed in more then one place, as an example I will say that the user needs to calculate a number with a high degree of precision over and over again. This is where custom libraries come in, the user can create their own library for high precision numbers. So lets say the user has made their high precision functions, now what? They do not want to type in the code every time it is needed, and they want to use it in other projects too. The best option is putting it into a library. This allows the user to run the executable without having to have a copy of the library, however this does have the draw backs of having to recompile every time a change is made and it increases the size of the executable.

To make a library in a Linux environment first we need to have our functions be in C code and ending with .c, it is easier if we have them all in one directory. We then send the .c files to to compiler to change them into object files. To do this we can enter into the terminal gcc -c “filename”.c this tells the compiler to to create an object file. Specifically its the argument -c that does this. We now have an object file. To speed things up lets say that all the .c files are in one directory and nothing else is in there. We can type gcc -c *.c to change all the .c files into object files. Object files end in .o, we can then enter into the terminal the command ar lib”library_name”.a file1.o file2.o and so on. Where “library_name” is the name of the library, it is proceeded by lib and followed by .a, and the name of the library is followed by the files we want to include, “file1.o, file2.o” and so on”. Alternatively we can enter ar lib”library_name”.a *.o and get all the .o files in the directory if we set it up properly. This puts all the files we specify into the library creating a collection of useful functions for us to use. We can use the command option rc with ar to update a library, for example ar rc lib”library_name”.a “update1”.o this may be necessary if a bug is found, but then we have to recompile all the files using the library.

How do we use a library we have created? We first have to have a file we want to use the library in, we then call a function in that file. For example if one of our functions where foo, we would need to call foo in the file we want to use the library in. We then need to compile the file with the library. To do so we can enter the following into the terminal: gcc “file_to_use_library_in”.c -L. -l”library_name” -o “executable_name” this tells the gcc compiler to use the linker to link the “file_to_use_library_in”.c with the “library_name”, the -L(dot) tells the compiler where to look for the library, specifically in the current directory, the (dash)l in front of “library_name” tells the gcc compiler its a library. Finally the -o option tells that we want the output, the executable to be named “executable_name” this process is called compiling a file. Specifically we are linking the file with the library we made and then turning it into an executable file for the user.

--

--