What About Libraries

Arquimedes
3 min readJul 6, 2020

We usually know a library like a curated collection of sources of information and similar resources, selected by experts and made accessible to a defined community for reference or borrowing, often in a quiet environment conducive to study.

And what about a library on C?

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.

Libraries may call functions in other libraries such as the Standard C or math libraries to do various tasks.

A static library work as the compiling process

Let’s create a Static Library

  1. Compile the library code into an object file so we tell GCC to do this using -c ( You can use your GNU Compiler)
$ gcc -c *.c

2. After this all the .c extension files( C files) in the current working directory have been converted in to their respective object files. Once we have object file(s), we use the GNU ar command to create our final library/archive

$ ar -rc libholberton.a *.o

3. This tells ar to create an archive (option c) and to insert the objects, replacing older files where needed (option r)

$ ranlib libholberton.a

This step may or may not be necessary depending on your computer system or your archiver(not necessary with ar).

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

ar -t libholberton.a

You can see the symbols, with the command:

nm lib_example.a

After this you created a static library libholberton, and now you can used it with the linking process.

gcc main.c -L. -lholberton -o main

Now run the executable program ‘main’

$./main

Executables generated using static libraries are no different than executables generated from individual source or object files. Static libraries are not required at runtime, so you do not need to include them when you distribute your executable. At compile time, linking to a static library is generally faster than linking to individual source files.

Go ahead and try to do your static library!

--

--

Arquimedes

Beginner Programar, social comunicator. Love pop music, family and friends.