Static Libraries in C
Holberton school project

Static Libraries in C

• Static libraries have the extension: .a

Anything linked in the static library will compile directly in the the final object

• Why is it useful?

After compiling, the library file does not need to reside on the target platform for the executable to run, that's really good, no missing libraries for the users woohoo!

• How to create a Static library?

Having a bunch of written functions in one folder (.c files), one may use this command

gcc -Wall -pedantic -Werror -Wextra -std=gnu89 -c *.c        

to turn those functions into object files (.o)

lets say we want to create a library called test, we can use all of the new object files and create a static library with this command

ar -cr libtest.a *.o        

• ar is the archiver: it is what we use to create the library,

• -cr means to create the archive or to recreate it, it is both the options r and c for the ar program

• libtest.a is our output file. by convention, libraries start with lib.

• *.o is selecting all of the object files

Now that we created our static library, we could use the library to compile with our main function, keep in mind you STILL need to declare your prototypes in the header file.

gcc main.c -L. -ltest        

This command compiles our main.c with the test library that is in the current folder.

We declare the folder by using the -L option for gcc and the library name with the -l option


To view or add a comment, sign in

More articles by Youssef Jellouli

Explore content categories