C static libraries as an essential tool for software engineers. Is this article we will covert the most important things you need to know about then:

  • Why use libraries
  • How they work
  • How to create them
  • How to use them

Why use libraries

A library is a file containing several object files, that can be used as a single entity in a linking phase of a program — source.

If you’re not familiar with how C programs are compiled have a look a this first.

In software engineering, you tend to break a large problem into several little problems and then solve a little problem at the time. In other words, you will code a function at the time.

What often happens is that some functions are to be used several times throughout a project. However, in C language, this process could be very time consuming because you must explicitly link the function to every program that uses it.

Hence libraries.

They are particularly useful for storing frequently used routines because you do not need to explicitly link them to every program that uses them. The linker automatically looks in libraries for routines that it does not find elsewhere — source.

You can use two types of libraries: static or shared.

How they work

A static library is the result of the linker making copy of all used library functions to the executable file. Static Linking creates larger binary files, and need more space on disk and main memory. Examples of static libraries (libraries which are statically linked) are, .a files in Linux and .lib files in Windows.

How to create them

1. Create a C file that contains functions in your library.

/* Filename: 2-strlen.c */#include "holberton.h" /**
* _strlen - the length of a string
* @s: the string
*
* Return: the lenght of the string
*/
int _strlen(char *s)
{
int result = 0;
while (*s != '\0')
{
result++;
s++;
}
return (result);
}

Here there is only one function for simplicity but you can more!

2. Create a header file for the library

/* Filename: lib_mylib.h */#ifndef HOLBERTON_H
#define HOLBERTON_H
#include <stdlib.h>
#include <stdio.h>
int _strlen(char *s);#endif

3. Compile library files.

gcc -pedantic -Wall -Werror -Wextra -c *.c

This will compile all your files ending with “.c” into “.o” object files.

4. Create static library.

ar -rc liball.a *.o

This will take all the object files and bundle them into a static library called “liball.a”.

If needed use the “ranlib liball.a” to index the library.

How to use them

1. Create a C file with main function

/* filename: test_lib.c  */#include "liball.h"void main(){
_strlen("How long is this string?");
}

2. Compile the test_lib program.

gcc -c test_lib.c

3. Link the compiled test_lib program to the static library.

gcc test_lib.c -L. -lall -o test_lib

‘-L’ specifies the library path.
‘-l’ goes before the library name.

4. Run the test_lib program

./test_lib

And your output should be:

22

👨🏼‍💻 Entrepreneur and software engeneer // Former employee at TheFamily.co // 👨🏼‍🎓 Holberton School & Reverse Origins