What is a C Static Library?

What is a C Static Library?

No hay texto alternativo para esta imagen

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities.


No hay texto alternativo para esta imagen


Static libraries aren’t loaded by the compiler at run-time; only the executable file need be loaded. Static library object files instead are loaded near the end of compilation during its linking phase.

Using a static library means only one object file need be pulled in during the linking phase. This contrasts having the compiler pull in multiple object files (one for each function etc) during linking. The benefit of using a static library is that the functions and other symbols loaded into it are indexed. This means instead of having to look for each entity on separate parts of the disk, the program need only reference a single single archived object (.a) file that has ordered the entities together. As a consequence of having one ordered object file, the program linking this library can load much faster.

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


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.
$ gcc -c *.c
No hay texto alternativo para esta imagen


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.

$ ar -rc libholberton.a *.o
No hay texto alternativo para esta imagen


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.

$ ranlib libholberton.a
No hay texto alternativo para esta imagen


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

No hay texto alternativo para esta imagen


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

No hay texto alternativo para esta imagen
ar -t libholberton.a
No hay texto alternativo para esta imagen

In the end, the static function tool is a great boon as it not only offers a simplified compilation process and fewer resource use, but also reduces the programs size taking up less space on the disk.

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

ROBERTO PALACIOS的更多文章

  • What Happens When You Type Into a Browser and Hit Enter?

    What Happens When You Type Into a Browser and Hit Enter?

    TERMINOLOGY Before I begin, allow me to define three terms that will be relevant for the duration of this article:…

  • WHAT IS IOT?

    WHAT IS IOT?

    Internet of Things Since the arrival of the Internet in our lives, it has evolved in a fast and overwhelming way, from…

  • Machine Learning

    Machine Learning

    Due to increased capacity and lower cost of information technologies and sensors, we can produce, store and send more…

  • Mutable, Immutable... everything is object!

    Mutable, Immutable... everything is object!

    Mutability and immutability in Python (and in other programming languages), that the variables can change (mutable) or…

    2 条评论
  • Statics Libraries VS Dynamics Libraries

    Statics Libraries VS Dynamics Libraries

    As we make computer programs, we realize that some parts of the code are used in many of them. For example, we can have…

  • What happens when you type ls -l in the shell

    What happens when you type ls -l in the shell

    Unix is a powerful system for those who know how to master its power. In this chapter, I will try to describe several…

    4 条评论
  • What happens when you type gcc main.c

    What happens when you type gcc main.c

    Compiling simple programs Say you have a file hello.c as follows : #include void main () { printf("hello"); }…

  • What is the difference between a hard link and a symbolic link?

    What is the difference between a hard link and a symbolic link?

    Now, we see that both hardlink.file and source.

  • What happens when you type ls *.c

    What happens when you type ls *.c

    Note: This post assumes you are familiar with the general concepts of files, directories, filename extensions, how…

社区洞察

其他会员也浏览了