Static Libraries in C

Static Libraries in C

Welcome to my third post!

This time I will talk about Libraries in C. I will tell you what, why, how and then some examples about libraries

What are the libraries?

Libraries are a group of files that have a functionality pre-built by third parties, and that can be used by any executable. Libraries contain variables and functions within them. It is known as library (or libraries) to certain types of files that we can import or include in our program. These files contain the specifications of different functionalities already built and usable, such as reading from the keyboard or showing something on the screen, among many others. By being able to include these libraries with definitions of different functionalities, we can save a lot of things and time.

Some examples of popular libraries are:

  • stdlib contains a lot of common functions such as atoi.
  • math a library of functions that perform math calculations.
  • stdio contains functions that read or write to a file and more.
  • time the library contains functions to work with time.
No alt text provided for this image


Static and Dynamic libraries

There are two types of libraries, dynamic and statics, in this post we will focus on the Static libraries.

A static library (.a file on linux) is loaded when the program is compiled. The necessary functions of that library are copied into your executable. If you take the executable from one computer to another, the program will work anyway, even if the library is not on the new computer, since the executable has its own copy. The problem is that the executable will be larger, since it carries a copy of the library's functions.

A dynamic library (.so file on linux) is loaded at the time of program execution, as needed. The executable does NOT carry a copy of the library's functions and it needs the library to function. If you take the executable to another computer, you must also take the library with you or make sure it is already there. The advantage is that the executable is usually smaller.

The libraries that are used in many programs or that are in the operating system are usually dynamic, since it is easy for the computer to have them installed and it saves you from having several copies of them in several executables.

The libraries that a single program uses is better to make them static, unless they are many and very large and they generate a gigantic executable.

Because the static library object file (.a) is linked at the end of compilation, each program that requires this library must have its own copy of the relevant library. Unlike the static library, a single shared dynamic library can be loaded into memory at run time and accessible to other programs linked to the same library.

The compiler does not load static libraries at run time; you only need to load the executable file. Instead, static library object files are loaded near the end of compilation during their link phase.

How to create a static library

1.  Put all relevant files into one directory including the header (.h ) file containing prototypes for the relevant entities. Make sure that the header (.h) file contains the macros #ifndef <HEADERFILE>_H and #define <HEADERFILE>_H at the top and #endif at the bottom so that the header file is only defined once instead of each time it is called.

No alt text provided for this image

2. Batch compile all source (.c) files. Use the -c option so that compiler doesn’t link the object files yet but instead creates counterpart object (.o) file for each source (.c) file.

No alt text provided for this image

3. Archive all of the object (.o) files into one static library (.a) file. Use the command option -r to ensure that if the library (.a) file already exists, it will be replaced. The command option -c should be used so that if the file doesn’t exist, it will be created.

No alt text provided for this image

4. Move the library (.a) file into the same directory that the entry-point file resides.

No alt text provided for this image

5. Now, instead of having to include all relevant file names in the compilation command, only the library (.a) file need be referenced.

No alt text provided for this image

One more thing ;)

If you need it, you can use gcc (the compiler) to link a library file or to look for a library, the option flags you need are -L and -l.

gcc -l links with a library file.

gcc -L looks in directory for library files.

And thats it!


Now, you can index and list the contents of an archive: 

ranlib command in Linux is used to generate index to archive. ranlib generates an index to the contents of an archive and it will be stored in the archive. The index lists each symbol defined by a member of an archive which is simply relocatable object file. You may use nm -s or nm –print-armap to list all of this index. An archive with such an index speeds up the linking to the library and allows routines in the library to call each other without regard to their placement in the archive.

No alt text provided for this image

Syntax:

ranlib [--plugin name] [-DhHvVt] archive

Example: It will generate index to archive as shown in the below example.

No alt text provided for this image

Thanks!

要查看或添加评论,请登录

Aaron Valiente的更多文章

社区洞察

其他会员也浏览了