STATIC LIBRARIES C

joel silva
3 min readOct 13, 2020

--

Why use libraries?

Good development practices with the use of patterns. They are based on design patterns, which indicate guidelines on how to solve a specific problem that has already occurred before.

Minimizing errors and making it easier to solve possible errors that you may have will always be better than developing it from scratch.

Ease of finding code that already covers functionalities of a particular development.

Facilitates collaboration with other developers.

It facilitates maintenance when the application needs to be updated or an evolutionary one needs to be carried out, taking less time and reducing the total cost.

Structure and organization of the default code, avoiding having to perform an analysis on where to place the different application files (resources, controllers, views, models, etc.).

Code reuse. Avoid code duplication.

Agility and speed in development. Thanks to the reuse of code we achieve faster development, since we will not waste time developing new functionalities.

Lower cost in development. Finishing a project earlier means that dedication is less and the cost of a project decreases.

How to create them

The basic tool used to create static libraries is a program called ‘ar’, for ‘archiver’. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. In order to create a static library, we can use a command like this:

How to create them

just use the command ar -rc libholberton.a * .o to create a static library named “libholberton.a” and put copies of the object files in it. If the library file already exists, it has the object files added or replaced if they are newer than the library files. The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells you to replace the oldest object files in the library with the new object files.

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 (this will be better understood when we take a deeper look at the link process at the end of this tutorial). The command used to create or update the index is called ‘ranlib’, and is invoked as follows:

ranlib libutil.a

On some systems, the archiver (which is not always ar) already takes care of the index, so ranlib is not needed (for example, when Sun’s C compiler creates an archive, it is already indexed). However, because ‘ar’ and ‘ranlib’ are used by many makefiles for many packages, such platforms tend to supply a ranlib command that does nothing. This helps using the same makefile on both types of platforms.

How to use them

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’. The -L option. instructs the compiler to look for libraries in the current directory (.). The -lfoo option tells the compiler to use a library named libfoo.a (or a shared library, libfoo.so, if one exists). To see what functions are included in an object file, a library, or an executable program, you can use the nm command.

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.

--

--

joel silva
joel silva

No responses yet