Libraries with code instead of books-

All about static libraries in C

See last step: Linking
  • Static library. (lib) A Static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static.
    They are usually faster than the shared libraries because a set of commonly used object files is put into a single library executable file. One can build multiple executables without the need to recompile the file. Because it is a single file to be built, use of link commands are simpler than shared library link commands, because you specify the name of the static library.
  • Dynamic or Shared libraries are .so (or in Windows .dll, or in OS X .dylib) files.
    These are linked dynamically simply including the address of the library. Dynamic linking links the libraries at the run-time. Therefore, all the functions are in a special place in memory space, and every program can access them, without having multiple copies of them.
image from:

Why use libraries?

This is where libraries come in handy. Instead of defining functions in every file, functions can be bundled up in a library file. This file can then be called within the program once and the functions can be used as many times as needed in the program.

How do they work?

Libraries can be default by the system or user created. Either way, they have to be declared in a header file and included atop the program where the library is being used.

How to create a static library?

The first step to create a static library is to gather all the .c files we want to include in out library and place them in the same directory. Like so:

  • The rflag will replace older object files in the library, with the new object files, and the cflag tells ar to create the library if it doesn’t already exist.
  • mylibrary is the name of the library to be created with my original functions inside. This means that if I wanted to use my functions, instead of defining every function inside my code or linking each function independently, I could use them through the library.
  • <filename>.o is every name of every object file I want to include in my library.

How to use a static library?

Now that the library is created with whatever functions we wanted to include, it is now available to use by any other user and program.

  • gcc is the compilation command
  • my_program.c is the program I want to compliate
  • -L. will tell the compilator to look in this directory for library files. It opens the option that a library will be looked for.
  • -l (mind the caps!) will be followed by the name of the library we created (without the “lib” and the extension “.a”- -l will handle this info)
  • -o my_program indicates the output file name.



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