Libraries In C What Are They Good For And all The Things That Got You Started

Ahmed Belhaj
4 min readOct 11, 2020

--

I -Introduction

A library in computer science is a collection of code routine that contain function ,variable, and classes …etc which the compile call in the linking phase producing a program the compile call the the library that contain a bunch of function that already optimized and ready to use the are two type of library

  • static libraries
  • shared (or dynamic) libraries

in our case we will look into static library and why you use them how do the work and how do we create one and how do we really use a libraries in our code

II-the Why

One advantage of static libraries is that you only have to distribute the executable in order for users to run your program. Because the library becomes part of your program, this ensures that the right version of the library is always used with your program. Also, because static libraries become part of your program, you can use them just like functionality you’ve written for your own program.As a programmer you main find yourself using the same function over and over again so its better to use a library instead of writing the same function a million time

III-The How

When a program is compiled, the compiler generates an object file from a source file. After generating the object file, the compiler also invokes the Linker. The role of the linker, in this case, is to copy the code of the library to our object file.

Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable.

Conventionally, they start with “lib” and end with “.a” in Linux or “.lib” in Windows

IV- Creation

To create a static library you have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .

lets say that we are in a folder with a bunch of .c function the first step is to make those source code .c function into object file .o

$ gcc -c -Wall -Werror -Wextra *.c

Once we have object file(s), we can now bundle all object files into one static library. so now we will use an archive program for now we will stick with ar (archive program in GNU)

$ ar -rc libname.a *.o

in this case we did select all the object files (.o) and made a library called libname.a

The ‘c’ flag tells ar to create the library if it doesn’t already exist. The ‘r’ flag tells it to insert object files or replace existing object files in the library, with the new object files.

third will need to index the library .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 will not matter during compilation. There are two ways to create or update the index.

$ ranlib libname.a

if you want to see the object files in our library you can use

$ ar -t libname.a

Example

we have a bunch of function in our folder

now we will make object files from them

after that we will create a library called libholberton.a

and now indexing and displaying whats in it

V- How to use them

Now our static library “libholberton.a” is ready to be used. we can use it in a program. This is done by adding the library’s name to the object file(s) given to the linker. First, let us create a C source file that uses the above created static library.

we have a c source file that use a _puts function that we created and archived in library

Example

example of code that use _puts

To use the static library call on the library during the linking phase of program compilation.

$ gcc main.c -L. -libname.a -o main

‘-L’ specifies the library path.

back to the example

I hope this article helped you become more than familiar with static libraries and C libraries in general.

--

--

Ahmed Belhaj
Ahmed Belhaj

Written by Ahmed Belhaj

Dedicated DevOps Engineer with expertise in system administration, core development, and a full-stack software development.

No responses yet