Libraries static in C?

Cristian Bedoya Blanco
3 min readJul 7, 2020

A library in C is a collection of header files, exposed for use by other programs. The library, therefore, consists of an interface expressed in a .h file (named the “header”) and an implementation expressed in a .c file. This .c file might be precompiled or otherwise inaccessible, or it might be available to the programmer. (Note: Libraries may call functions in other libraries such as the Standard C or math libraries to do various tasks.

So static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ".a’ suffix. This collection is created using the ar (archiver) program. Static libraries aren’t used as often as they once were, because of the advantages of shared libraries (described below). Still, they’re sometimes created, they existed first historically, and they’re simpler to explain.

There is another type of library called Dynamic Library( or Shared Library), but more on that later, for now, we will discuss about Static Library in detail.

A static library or statically-linked library is a set of routines, external functions, and variables which are resolved in a caller at compile-time

Now we going to create a static library called “library1.a”

The first step is basically is to compile the file using GNU compiler collection. “gcc *-c .c” and the option -c that means Compile or assemble the source files, but do not link. The linking stage simply is not done. Then make the file with a “.o” extension.

Secondly, the GNU arprogram creates, modifies, and extracts from archives. An archive is a single file holding a collection of other files in a structure that makes it possible to retrieve the original. Options -rcinsert the files member into an archive (with replacement) and create the archive respectively. So they take file with “.o” in the “library1.a”

Next “ranlib” generates an index to the contents of an archive and stores it in the archive.

Finally if we want to see the contents of our library, we can use the ar option -t.

We can also see the symbols in our library, using the command nm, which lists each symbol’s symbol value, symbol type, and symbol name from object files

We’ve just created a static library and now let us use the static library by invoking it as part of the compilation and linking process when creating a program executable

gcc main.c -L. -llibrary1 -o main

Where -L. specifies the path to the library.

We got the main.c ready to run! Awesome! right?

--

--

Cristian Bedoya Blanco

Materials Engineer and Software Developer. First that a professional i am humanistic person. I accepts and overcomes every single challenge.