How to Create Static Library in C and How to Use it.


Static Library in C

When compiling a C program, the compiler generates object code. (What is an object file? In our case, it is a file(s) that contains object code and has a .o extension.) Then, the compiler also invokes the linker. One of the main tasks of the linker is to make the code of library functions (eg printf(), scanf(), sqrt(), ..etc) available to your program. This task can be accomplished in two ways by the linker: By copying the code of library function to your object code, Or: By making some arrangements so that the complete code of library functions is not copied, BUT made available at run-time.

By convention, the static library has the prefix lib and the suffix .a Example: lib_myLibrary.a

Steps to create a static library

step1. Create a C file(s) that contains functions in your library.

atoi.c file

Note: For simplicity purposes, have created only one file, you can create multiple files as well.

step2. Create the header file for your library

Header file(It contains all function prototypes)

step3. Compiling your library file(s)

Generating object files

step4. This step bundles up multiple object files in one static library (check ar for further details). The output is a static library/archive. The archiver, also known simply as ar, is a Unix utility that maintains groups of files as a single archive file.

Creating a lib_myLibrary.a (Library)

And yey! our static library is ready to use!

Note: rcs r means that if the library already exists, replace the old files within the library with your new files. c means create the library if it did not exist. s can be seen to mean 'sort' (create a sorted index of) the library, so that it will be indexed and faster to access the functions in the library. Therefore, rcs can be seen to mean replace, create, sort

You may use this modifier flag (i.e s ) either with any operation or alone. Running “ar s" on an archive is equivalent to running ranlib on it.

So, Whenever files are added to a library, including the initial creation of the library , the library needs to be indexed, which is done with the command ranlib. ranlib makes a header in the library with the symbols of the object file contents.This helps the compiler to quickly reference symbols. A large library may have thousands of symbols meaning an index can significantly speed up finding references.

$ ranlib lib_myLibrary.a

The above 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 lib_myLibrary.a

For one to see the symbols in our library, we can use the command nm, which lists each symbol’s symbol value, symbol type, and symbol name from object files.

nm lib_myLibrary.a

Using Static Libraries:

For purposes of the demo, we are going to keep our library in our current directory.

Now, Let’s create a test program that uses the above-created library.

1. Create a C file with the main function

Creating a test program

2. Compile the test program.

Compiling the test program

3. Link the compiled test program to the static library. Note that -L. is used to tell that the static library is in the current folder (for further knowledge check this for details of -L and -l options).

The linker between the compiled test program and our lib_myLibrary

4. Let’s run the test program

That is how static libraries are created and used in nutshell.

Finally, thanks for reading this article. I enjoyed writing it, and I hope you enjoyed reading it as well. Let me know what you think in the comments box!

Till next time happy coding! 👩‍💻 ✌️

Watch out for part2 on how to create and use a dynamic 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