C STATIC LIBRARIES

Facundo Diaz
2 min readOct 10, 2020

--

Good afternoon in this blog I am going to talk about static libraries in c, what they are, what they are for, how to create and use them.

Static libraries, also known as object-libraries, are collections of object-files within a single file, generally with a .lib or .a extension.
They are always accompanied by header files, generally .h, which contain the declarations of the objects defined in the library. Then, during the linking phase, the linker includes in the executable the modules corresponding to the functions and library classes that have been used in the application. As a result, such modules become part of the executable, in exactly the same way as any other function or class that would have been written in the body of the application.

  • Structure and organization of the default code
  • Code reuse
  • Agility and speed in development.
  • Lower cost in development.
  • Good development practices with the use of patterns.
  • Minimize errors and easier to solve them.
  • Ease of finding a library or code that already covers features of your development.
  • Facilitates collaboration with other developers.
  • Facilitates maintenance.

To create a library we must write this “ar rc (here the name of a library. a ) (here all files with extension .o separated by a space each )“

For example:

ar rc test.a file1.o file2.o

Option ‘c’ tells ar to create the library if it doesn’t already exist.

The ‘r’ option tells you to replace the older object files in the library with the newer object files.

Then in order to use our library the only thing we have to do is, in the header of our program include our library as we usually do #include (name of library) and voila, we can use any function that we have pre-created in our library.

--

--