C Static Libraries

Rodrigo Zarate Algecira
2 min readJul 12, 2021

Static libraries are compiled object files that hold all variables and functions an application needs to run (symbols), its written in object code, and are indexed.

  • Why use libraries

Several reasons are born in mind to use libraries, you can think of speed up the compilation of the program, save space, make sure that you have all the files your code needs to operate, also having the correct version, and so on.

  • How they work

C libraries store files in object code getting a copy of the functions so later one can compile using just one single file that has all of the symbols needed in one step.

  • How to create them

To create a static library use the ‘ar’ command (archiver program) there are some flags/options you can use to instruct the ar command on how to deal with the files already on the library if you’re making an update of it.

To create an archive named mylib.a from files the1.o, the2.o, the3.o, the following command would be used:

$ ar rcs mylib.a the1.o the2.o the3.o
  • How to use them

To compile a program that depends on the1.o, the2.o, and the3.o, one could do:

$ gcc main.c mylib.a

You can get deeper info at this link: https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html

--

--

Rodrigo Zarate Algecira

In the path of Programming #AgriculturaUrbana $rodrigozarate Instagram: agromerlin