f you create a program in the C programming language, you will most certainly come to appreciate the usefulness of libraries. To simplify, libraries contain lots of information that you can use over and over again in many programs, thereby making your life easier because you don’t have to recreate the same things over and over again. Libraries also allow your programs to be smaller and more focused on the task at hand.
How do libraries work?
When you want to use a program you have written in C, you need to compile it into a language that your computer will understand, that is, the compiler will translate your source code into object code. The compiler, really combines four processes which take place in order and are made effective through: the preprocessor, the compiler proper, the assembler and the linker.
It is at the linking phase that the links are made between your program and any necessary libraries. There are two important types of libraries, Static libraries, and Dynamic (or Shared) libraries.
When using a Static library, code from the library will be written into your program. However when using Dynamic libraries, rather than write the actual code into your program from the library, the only the memory address is written into your program so the necessary code can be retrieved from the Dynamic library at run time.
Differences between Static and Dynamic libraries: Advantages and Disadvantages:
When considering the advantages and disadvantages of Static and Dynamic libraries, we may want to consider size, speed, and updates. Since the actual code from Static libraries is written into your program, when it comes time to run the program, the code is already there, and so, your program may run slightly faster than programs that must, at run time, go and retrieve code at a given memory address. However, your object code files will be larger when using Static libraries, and if you have many of them, the added space taken up on the disk may be an issue to consider. Another issue with Static libraries concerns updates. If you need to update material in a Static library, the only way to update all the programs that already have the corresponding code written in them, is to re-compile those files. If they are few in number, this may not be an issue, but if there are many, Dynamic libraries may be preferable. When it comes to compatibility, Static libraries offer an advantage: since the source code contains the code from the library, there are not compatibility issues that arise from updates to a Dynamic library. So now, when we consider Dynamic libraries, we can see their advantages and disadvantages. A Dynamic library, while it may make your program run a little slower at run time, will save space on your disk because you will only be writing a memory address into your object code instead of the much longer code itself. And when you need to make updates, instead of recompiling all your files, you simply need to update the code in your Dynamic library. The programs that use it store only the memory address, so one update will suffice for all your programs. Cool, no?
How to create and use libraries in Linux:
To create a Static library, you start by assembling all your library files into one file. They should be object files, ending with the extension “.o” (example: foo.o). You will place them in your static library, which should end with the extension “.a” and begin with the letters “lib” (for example: libwhatever.a). To do this, you will need to use a program called “ar.” How do we obtain those files with the .o extension?
First, we need to compile our .c files in such a way that they pass through the preprocessor, the compiler proper and the assembler, but we will stop the process right before the linking stage. To do this, we will compile our files using:
gcc -c *.c
This command will compile all the files with the “.c” extension and create object code in “.o” extension files, but will stop short of the linking phase. For example:
file1.c file2.c file3.c
$gcc -c *.c
file1.c file 1.o file2.c file2.o file3.c file3.o
Now we can create our library using the archiver: “ar” command:
ar rc libwhatever.a *o
When we run this command, our Static library , “libwhatever.a” will be created. Now we can simply index our library using:
This will make it easier and faster to find what you need in your new Static library!
Use your library to store important programs that you use often or in various programs so that you don’t have to repeat the same work over and over.
Use your Static library:
Add the name of the library to the list of object files you will provide to the linker using the “-l” flag:
gcc main.o -l -lwhatever -o program
Notice that when referring to the library, you drop the initial “lib” prefix as well as the “.a” extension.
To create a Dynamic library, start by compiling your “.c” files with the following command:
gcc -c fPIC *.c
This command will take your “.c” files and return object code files ending in “.o”:
file1.c file2.c file3.c headerfile.h
$ gcc -c fPIC *.c
file1.c file1.o file2.c file2.o file3.c file3.o headerfile.h
Now we will create our Dynamic library from these files:
gcc -shared -o libwhatever.so *.o
Our Dynamic library is now created. How can we use it?
If we want to use a program, “printhappyface” in our file1.c file to run a program in our main1.c file, we can compile our main1.c file in the following way:
gcc -L. main1.c -lwhatever -o printhappyface
By adding -L., the linker will look for a library in the current directory. Although we wrote “-lwhatever” the linker will find the library libwhatever.so and use the information found there to produce “printhappyface”.
Now you can add the environment variable to the library path (so it can be found):