C static libraries

Why use libraries in C?

Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.

How do static libraries work?

Compilation Process

Static libraries are added during the linker phase of the compilation process (above). During the linker phase: the linker links access all the libraries to link functions to the program. Static libraries are merged with other static libraries to create an executable program. During the compilation of a program, the static library is called to execute the program.

How to create static libraries

have to use the ‘ar’ or archiver program. The idea is that these functions are being archived until needed. A function saved in .c format is recompiled into an object file, .o .

Creating a Static Library step by step:

Step 1.

Create all your source files. Source files hold any functions you will use.

Step 2.

Compile the source files into object files. Using GCC use this command:

$ gcc -c *.c

This will change the object files to source files.

Step 3.

Create a static library. Using “libholberton” as an example of a library name, this command creates a Static library.

$ ar -rc libholberton.a *.o

The ‘ar’ is the program being used to archive the files. The ‘c’ tells the program to create a library. The ‘r’ tells the program the replace or update older files in the library.
With step three a static library has been created.

If needed use the ‘ranlib <libraryname.a> to index the library.

$ ranlib libholberton.a

Indexing a library will let the compiler know just how old a library file is. Indexing also gives the library file a header and makes it easier for the compiler to reference the symbols. This step may or may not be necessary depending on your computer system.

How are static libraries used?

We have created a static library and can now use it.

Step 1.

Write a program:

#include "holberton.h"int main(void)
{
_puts("\"My Static Library is working.\"");
return (0);
}

In the header: “holberton.h” contains function and function definitions used to tell the compiler how to call functionality. It contains “data types and constants used with the libraries”(geekforgeeks). When we compile the file we call the library:

$ gcc main.c -L. -lholberton -o quote

Now we can run the executable:

$ ./quote

If the library is working properly the output should be:

"My Static Library is working."

We have created a functioning static library.

I hope this article helped you become more than familiar with static libraries and C libraries in general. They are very useful tools in programming in C and C++. Since the beginning of C programming, there was a need for libraries, a place to store the commonly used functions in C for easy access and use by programmers using C. Creating a library tailored to your personal programming needs may save you a lot of time during compilation and programming. We have discussed how static libraries work, and in encourage you to investigate how dynamic libraries may be of use to you as well.

--

--

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