Example Project

Creating a library

In an earlier set of lecture notes I showed code to encode ints in base 64. As an alternative to adding this code as source code in a project we can also compile this code into a library and then link the library into any project that needs it.

These lecture notes will cover the process of creating and using a library on Linux.

We already have a C source code file and an accompanying header file for the code we need. The only change we are going to make is in how we compile the code.

Here is the code for a new makefile I will use to compile the code into a library and then install that library on our system:

test : library main.c
  gcc main.c -lbase64 -o test

library: base64.o
  gcc -shared base64.o -o libbase64.so

base64.o : base64.c
  gcc -c -fpic base64.c

install: library
  mkdir /usr/include/base64
  cp base64.h /usr/include/base64/base64.h
  cp libbase64.so /usr/lib/libbase64.so

When you construct a library on Linux you have two choices for what kind of library file to make. The first option is to compile the code into a static library in the form of a .a file. That static library file then gets linked into any project that needs it. The second option, which is the option we are going to use here, is to compile the code into a shared library in the form of a .so file. Shared libraries get soft linked into executables: in a soft link the library code stays in the .so file and does not get copied into the executable. Instead, the executable loads the shared library file into memory at run time. The advantage to this second method is that one shared libary file can be used by multiple executables. This saves space on the computer.

To compile code into a library we first compile the base64.c source code file into an object file named base64.o. The compiler command that does this uses the -c switch to tell gcc to just compile base64.c into an object file and not into an executable file. The object file we create in this step can eventually be linked into an executable, or it can instead be linked into a library file. To do the second option we also use the -fpic switch to compile the code into a position independent code file.

To link the object file into a shared library file we then run gcc again with the -shared option to tell it to link base64 into a shared library, libbase64.o. The naming convention used for shared libraries in Linux is to start their names with 'lib'.

To compile our library we now run the command

make library

Installing the library

Our library now consists of a shared library file and an accompanying header file. To make both of these things useful we now have to copy these things into a couple of standard locations on our system.

When someone wants to use our library they will start by putting an include statement

#include <base64/base64.h>

into their source code file. Note the use of the <> style include here. When the C compiler reads an include statement with <> it will try to search for the requested header file in the system header area. On most Linux systems that system header location is /usr/include. To install our library we will have make a folder named base64 in /usr/include and then copy our header file to that folder.

To use the shared library we created we will need to add a compiler switch when we compile our project to tell gcc to link a particular library into the project. In this case, we will use the -lbase64 switch. When gcc sees this switch it will go looking for a shared library file named libbase64.so. It will search for that shared library in system location where shared library files are stored. In most Linux systems there are several locations where shared libraries can be stored. One such location is the /usr/lib directory. This is where we are going to store our shared library.

To install our header file and our shared library file in the right locations I have added an install target to the makefile. The code in this section of the makefile creates the /usr/include/base64 directory and then copies the header file and the library files into the correct locations.

To copy everything into the right location we simply run the command

make install