C - Static libraries

C - Static libraries

WHY USE LIBRARIES

One of the tools that compilers supply us with are libraries. 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.

HOW THEY WORK

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.

HOW TO CREATE THEM

  • First you will need your entire collection of .c files with the functions you are going to include in your library, like this:

No alt text provided for this image

  • When you have all your files with the functions, the next step is to compile them:

No alt text provided for this image

  • 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:

No alt text provided for this image

This command creates a static library named 'libholberton.a' and places copies of the *.o' object files in it. If the library file already exists, it has the object files added or replaced, if they are newer than those inside the library. The 'c'flag tells ar to create the library if it does not already exist.'r'

  • 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:

No alt text provided for this image

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'. Here is an example:

No alt text provided for this image


This will create a program using object file "main.o", and any symbols it requires from the "holberton" 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.

To view or add a comment, sign in

More articles by Shannel Bejarano

  • Activation Functions

    The activation function returns an output that will be generated by the neuron given an input or set of inputs. Each of…

    1 Comment
  • How to fix HTTP 500 Internal Server Error

    This Postmortem article is based on one of my Holberton School projects, where we have to fix a server which has a 500…

    2 Comments
  • ¿ What is IoT - Internet of Things ?

    The Internet of Things (IoT) is the collection of interconnected electronic devices and sensors that measure, collect…

    1 Comment
  • What happens when you type a command in the Shell?

    first let's go step by step: What is a Shell? A Shell or also known as a Command Interpreter, terminal, prompt, cmd, or…

    1 Comment
  • C COMPILATION PROCESS

    The word compile means to translate programming code into machine executable code. There are different ways to compile…

Others also viewed

Explore content categories