An Intro to Libraries in C

Holden Grissett
3 min readNov 7, 2016

--

C programming is fun, but it can also be tedious. Compiling programs with other program prototypes in the head of your file is fine for small things, but for big projects, this would be torture.

Libraries are a useful tool that allows us to tidy up our directories so that our object files can be stored in one neat little file.

There are two types of libraries on most systems: static and dynamic.

Static libraries are named this because they are simply collections of object files. They are added to other object files during the linking phase of compilation and that’s it.

Dynamic libraries are linked as well, but their object files are not added to the output file; instead, when the program starts, the ‘dynamic loader’ checks out the the necessary files and loads them up temporarily with the object file. This feature is slightly slower but saves on memory because there only needs to be one copy of the library.

In this introduction, we’ll discuss static libraries. I’ll show you how they work, and how you can use them.

How do I create a static library?

Static libraries are actually fairly simple. These simple steps walk you through a simple way of creating one.

  1. First, make sure all of the C files you want in your library are in one directory. Make sure all of them work correctly, or your library won’t work either!
  2. Compile all of your C files up to the linking phase using this command:

gcc -c *.c

The ‘-c’ option compiles up to the linking phase of compilation, so you can add them as object files (.o) to your library. The ‘*.c’ argument specifies any file ending in ‘*.c’.

3. Now you have your object files! Use the command:

ar -rc libmyfiles.a *.o

This links all of the object (.o) files in your current directory. ‘ar’ is a command that archives any files you give it (libraries are actually archive files). ‘-rc’ will ‘replace’ old object files with new ones and ‘create’ a new archive file if it doesn’t exist. ‘libmyfiles.a’ needs to have the ‘lib’ and ‘.a’ parts no matter what; the middle is your library name. ‘*.o’ grabs all of the object files you created and adds them to the library.

4. You’re all done! Actually, one more thing… The library still may need to be indexed. It depends on the compiler, but just to be safe, use ‘ranlib libmyfiles.a’ to index them. This speeds up the look up process when the computer looks for a certain object file.

How do I use a static library?

Once you make your library, it’s very simple to use. If you don’t use gcc, just do this with gcc replaced with your compiler.

Just add it as an ‘-l’ option to the file you want to compile like so:

gcc main.c -L. -lmyfiles

This will compile ‘main.c.’ When it gets to the linker, it is told via ‘-L.’ to look for a library in the ‘.’ (current) directory by the name of ‘-l’ ‘myfiles’ to add to the output file. The ‘-l’ option automatically adds ‘lib’ and ‘.a’ to the library, so you only need to give it the middle portion of the file name.

That’s it! Libraries can make big directories cluttered with programs much less cluttered. They come and handy and will save you a lot of time and stress. Enjoy!

--

--

Holden Grissett

Software student @holbertonschool, Co-founder @hango, simultaneously Striving & Thriving