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.

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store