What are C static libraries?

Luis Alexander Obregon Mogollon
2 min readJul 10, 2021

Hello fellow readers, it’s been a while since I’ve written a blog about code. The last subject I focused on was the process of compilation, but today I will be talking about static libraries: Why we use them? How they work? How to create them and how do we use them?

In short, Static libraries are files that contain several files which in them have only 1s and 0s, that only the computer understands (aka object files). They can be called by your program which makes easy to access your functions, variables, etc. The collection of object files are linked during the linking part of compilation process. You can learn more about compilation here.

How to create a static libraries?

To create a static library you just need to compile your library code and stop it before the linker. This will give you an object code and you can use the command GCC -c to help you.

gcc -c *.c

gcc is the compiler, -c will tell your compiler to stop right before the linker step and *.c will select all the .c files you want to compile.

After you compiled all your .c files you want, you should have files ending in .o which stands for object codes. Now that you have your object codes, use the ar (maintains groups of files in an archive)command to create your final library. The options -rc allows you to create an archive and replace older files if needed.

ar -rc library_name.a *.o

When you create an archive file, you want to end your file name with .a your computer knows the file is an archive. The *.o in your code allows you to select all of the .o (files in 1 and 0s that only computers can read) and now you have your archive file.

ar -t archive_name.a

To see what is inside your archive file, use the command ar -t and it will list all the content in order from your library. That is basically how you create an archive (library). Hope you got a little better understanding from this.

--

--