What is a “C” library? What is it for?

What is a library, and how do they work?

How to create a library?

$ gcc -Wall -pedantic -Werror -Wextra -c *.c
$ ar -rc libholberton.a *.o
$ ranlib libholberton.a
$ ar -t libholberton.a
$ nm libholberton.a

Using Static Library

gcc program_code.c -l<name> -o program_code
gcc main.c -L. -lholberton -o main
  • -l option eliminates the need to type out the lib prefix and .a extension for the library. In other words, -l<string> replaces lib<string>.a
  • -L specifies the path to the library. We can use -L. to point to the current directory and -L/home/tmp to point to the /home/tmp directory.
  • If the code in the library is updated (for example, to correct a bug), you must recompile your program into a new executable.
  • Each system program that uses that library contains a copy in its executable; which makes it very inefficient and a hassle when a bug is found because it has to be recompiled, per point one.



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