Static Libraries in C

Static Libraries in C

What are C libraries?

One of the tools that compilers supply C programmers. A library file contains a collection of functions and declarations for use by other programs and programmers.

How may they be used? A team of engineers calculating the tension of a bridge may develop their own library of maths functions to complete common tasks.

Why use libraries in C? As a programmer, you may find yourself using the same function or functions repeatedly. On this case, is better put this function(s) into a library to speed up the program compilation. C libraries store files in object code; during the linking phase of the compilation process ((Compilation Process) <--- HOW DO STATIC LIBRARIES WORK) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.

Libraries make life easier

Libraries come in one of two flovars: DYNAMIC (Dynamic libraries are shared libraries with specific functions launched during the execution of a program and contribute to “reduced memory consumption”(techopedia.com). Dynamic libraries are linked in two stages. Static libraries produce object files and standalone executable files (wikipedia.org). These libraries can be linked to a program without recompiling the code.); STATIC (Static libraries date back to the creation of C itself. Static libraries house a collection of object (.o) files.)

Object files are products of the compilation phase and comprised mostly of machine code, but also contains information that allows the linkers to see what functions and global objects are located within the program.

Drawbacks of Static Libraries

For all of a static library’s efficiencies, it comes at a price. Because all object files within a static library are linked into the program during compilation, the resulting executables can get quite large. One can think of static library archives as similar to a zip file, but without the compression.

Secondly, if the library code is to be updated, a programmer must recompile their program into a new executable and every program that uses that particular library contains a copy in its executable.

How to Create Static libraries

No hay texto alternativo para esta imagen

Static libraries have the .a format. To create static libraries, we use a command called ar for archiver.

The first step is determining which functions we would like to include in our library and preparing them for the archiver — I like to place all the functions I like to use in a single directory. Next, we will compile all of our .c source files into .o object files by tagging on the -c flag:

$ gcc -c *.c

Once this command is entered, the corresponding .o files will be present in the directory. We are now ready to create our library file!

Next, we will take all of our .o files and package them into a single .a file.

For a full list of the capabilities of the arcommand, take a look at its man page (man ar) — its able to create static libraries, modify object files in the static library, list the names of object files in the library, and more.

No hay texto alternativo para esta imagen


Let’s suppose we will call our static library libsuper.a. We now have our necessary .ofiles. We will run the command:

ar -rc libsuper.a *.o

The -r flag tells the arcommand to create a library if it doesn’t already exist and replaces older existing object files in the library.

No hay texto alternativo para esta imagen

The -c flag tells the arcommand to create the library if it doesn’t exist.

No hay texto alternativo para esta imagen

We’ve created our static C library!

Now it’s time to employ our shiny new library.

Before we do so, let’s look at the linking option in the (lengthy) man page for gcc ( man gcc).

No hay texto alternativo para esta imagen

We specify the library to the compiler with -lname where name is the filename of the library without the prefix lib. Earlier, we created the library libsuper.a. We also specify the location for our libraries, namely the current directory using the flag -L.

Our final command to compile our program (we’ll name it prog in this example) is:

gcc main.c -L -lsuper prog

We have created a functioning static library.

I hope this article helped you become more than familiar with static libraries and C libraries in general. They are very useful tools in programming in C and C++. Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C. Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming.



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

Duvan Rodelo的更多文章

  • Ethical Challenges in AR / VR

    Ethical Challenges in AR / VR

    One of the big areas of ethical concerns surrounding augmented reality and virtual reality is privacy. These devices…

  • What happens when you type holbertonschool.com in your browser and press Enter

    What happens when you type holbertonschool.com in your browser and press Enter

    When you type an URL such as https://www.holbertonschool.

  • Internet Of Things

    Internet Of Things

    What is the Internet of Things? The Internet of Things, or IoT, refers to the billions of physical devices around the…

  • How recursion works & .... other stuff !

    How recursion works & .... other stuff !

    Recursion. The word alone brings back some painful and confused memory to every CS dev.

  • All stuff are Objects

    All stuff are Objects

    The first time when I hear about Pyhton , the message was "Python is dangerous". But in the coding way, like in the…

  • Making and Using Dynamic Libs (C)

    Making and Using Dynamic Libs (C)

    In order to explain the significance of using dynamic libraries, I’ll need to define some terms. First of all, a…

  • SCRUM Basics

    SCRUM Basics

    Whats a SCRUM ?, basically is team environtment to define objetives and personal contributions to a project…

  • What happens when you type ′ls -l*.c′ on your shell

    What happens when you type ′ls -l*.c′ on your shell

    What is a shell? First, let us define what is a shell. By Marrian-Webster (MW) a shell is: A hard rigid usually largely…

  • FANTASTIC 4 TO COMPILE IN C.

    FANTASTIC 4 TO COMPILE IN C.

    About the compiling language we have C. And you also know how to write a C program.

社区洞察

其他会员也浏览了