C static libraries đŸ€“

Lina María Montaño Ramírez
3 min readOct 14, 2019

--

Static libraries are collections of object files that are linked when a file is compiled into an executable file.

Why use libraries?

In a static library, the linker includes the object code of the functions of the library that uses its source code. If you create a static library called lib.a in your source code and then call the functions defined in lib.a, the implementation of those functions would be passed to object code and then included in the final executable file by a launcher.

How they work?

processes

How to create them?

  1. Listing the files with their respective functions .c
.c

2. compiles files where .o are created

gcc -Wall -pedantic -Werror -Wextra -c *.c
.o
  • gcc = integrated compiler, is able to receive a source program and generate a binary executable program.
  • Wall -Wextra -pendantic = flags are all warning and error flags that should be included when compiling your code
  • -c = link the files to be created as objects

3. Creation of the bookstore with ar

ar -rc libholberton.a *.o
ar
  • ar = This program can be used to create static libraries (which are actually archive files).
  • -r = replaces older files
  • -c = create the library if it doesn’t exist

How to use them?

The way we can visualize it is by means of nm

nm libholberton.a
nm
  • -nm = lists the value of each symbol, the symbol type and the symbol name of the object files.

We need to index our created file, this will help the compiler when searching inside the library this command is ranlib

ranlib libholberton.a
ranlib
  • -t = serves to view the content of our created library
ar -t libholberton.a
ar

Using a “C” library in a program

the use of the ‘-L’ flag — this flag tells the linker that libraries can be found in the given directory (‘.’, referring to the current directory), in addition to the standard locations where the compiler searches for system libraries.

gcc 0-isupper.c -L. -lholberton -o isupper
-L
./isupper
exec

Reference

--

--

Lina María Montaño Ramírez

Backend Developer đŸ‘©â€đŸ« Mentor @coderiseorg 🎧 Podcaster en @caminodev ! 💚 Organizer @node_co