Libraries in C

Smith Flores
6 min readSep 25, 2021

--

What are libraries and why use libraries in c ?

A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Normally the library is indexed, so it is easy to find symbols (functions, variables and so on) in them. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk. Also, when using a library, we have fewer files to look for and open, which even further speeds up linking.

Types of libraries in C:

There are two types of libraries in C.

Static libraries

Static libraries are just collections of object files that are linked into the program during the linking phase of compilation, and are not relevant during runtime. This last comment seems obvious, as we already know that object files are also used only during the linking phase, and are not required during runtime — only the program’s executable file is needed in order to run the program.

Shared or dynamic libraries

Shared libraries are linked into the program in two stages. First, during compile time, the linker verifies that all the symbols (again, functions, variables and the like) required by the program, are either linked into the program, or in one of its shared libraries. However, the object files from the dynamic library are not inserted into the executable file. Instead, when the program is started, a program in the system (called a dynamic loader) checks out which shared libraries were linked with the program, loads them to memory, and attaches them to the copy of the program in memory.

How static libraries work

What is the difference between them

Creating A Static “C” Library

First compile all your files *.c with “gcc” and the flag “-c” it would look something like this :

gcc -c *.c

This will create * .o files that will serve us to create the library, for that we need to use the following command:

ar -rc liball.a *.o

This command creates a static library named ‘libutil.a’ and puts copie of the object file “util_file.o” .If the library file already exists, it has the object file added to it, or replaced, if they are newer than those inside the library. The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object file in the library, with the new object file.

After an archive is created, or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library, and to make sure that the order of the symbols in the library won’t matter during compilation.The command used to create or update the index is called 'ranlib', and is invoked as follows:

ranlib liball.a

Note: when an archive file’s index generation date (stored inside the archive file) is older than the file’s last modification date (stored in the file system), a compiler trying to use this library will complain its index is out of date, and abort. There are two ways to overcome the problem:

  1. Use 'ranlib' to re-generate the index.
  2. When copying the archive file to another location, use 'cp -p', instead of only 'cp'. The '-p' flag tells 'cp' to keep all attributes of the file, including its access permissions, owner (if "cp" is invoked by a superuser) and its last modification date. This will cause the compiler to think the index inside the file is still updated. This method is useful for makefiles that need to copy the library to another directory for some reason.

How to use libraries in C

After we created our archive, we want to use it in a program. This is done by adding the library’s name to the list of object file names given to the linker, using a special flag, normally '-l'. Here is an example:

gcc main.o -L. -lutil -o prog

This will create a program using object file “main.o”, and any symbols it requires from the “util” static library. Note that we omitted the “lib” prefix and the “.a” suffix when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of a file to look for. Note also the usage of the '-L' flag - this flag tells the linker that libraries might be found in the given directory ('.', refering to the current directory), in addition to the standard locations where the compiler looks for system libraries.

Example:

Create a Static library from the following * .c files

First setp compiple with gcc and obtain files *.o:

using the command to create lirabrie with files *.o : ar rc libutil.a util_file.o

this created the file libHolbertonSchool.a, after this we can use ‘ranlib’ to generate the index , And our dynamic library would already be created

Creating A Dynamic “C” Library

The way to create a Dynamic Library in Linux is with the gcc command using the -c to generate the object files (.o) from the source files (.c) and the -fPIC to make the code position independent. Thus, the following command makes a bunch of .o files from each .c file in the current directory (You can select which functions you want for your library).

gcc -c -fPIC *.c

Next, we are going to put together those objects files into one library. To do this as a Dynamic Library we also use gcc but with the -shared option. The -o is to specify the name of the file you want it to have.

gcc -shared -o liball.so *.o

This way you must have your library created. To verify that you did it and have the right functions as dynamic symbols you can use:

nm -D liball.so

Great! at this point, you have your Dynamic Library created!

How to use it

Now, you have to compile the library with your main.c file to link it and use it as you want. For this to work, you have to add the location of your library files into the environmental variable to know where to find the functions.

export LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH

Then, you can compile it by typing the following:

gcc -L . 0-main.c -l all -o example

Note that the name we gave to the library in this example was ‘all’. Here we use the -L option to tell the program where to find the library, in this case . that refers to the current directory. The -l option is to tell the compiler to look for the library.

Example:

Create a Dynamic library from the following * .c files

Compile files *.c with gcc [ gcc *.c -c -fpic ]:

This created files *.o after this use [ gcc *.o -shared -o libHolberton.so ]:

This will produce the file libHolberton.so and that is all , enjoy your dynamic library :)

To see your librarie use the next command :

[ nm -D — defined-only libHolberton.so ]

Only those who risk going too far can discover how far they can go — T.S. Eliot

I hope this article has helped you understand about C libraries and their great importance in programming in C and C++. Grettings!)

--

--