Static Libraries
Axel Bouvier, Holberton School C#17

Static Libraries

Static libraries are simply a collection of ordinary object files; conventionally, static libraries end with the ``.a'' suffix. This collection is created using the ar (archiver) program. Static libraries permit users to link to programs without having to recompile its code, saving recompilation time. Note that recompilation time is less important given today's faster compilers, so this reason is not as strong as it once was. Static libraries are often useful for developers if they wish to permit programmers to link to their library, but don't want to give the library source code.

Why use static libraries?

The main benefit of using static libraries is execution speed at run-time. Because the it’s object code (binary) is already included in the executable file, multiple calls to functions can be handled much more quickly than a dynamic library’s code, which needs to be called from files outside of the executable.

How they work?

In computer science, 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. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable, or they may be loaded at run-time into the address space of the loaded executable at a static memory offset determined at compile-time/link-time.

How to create them?

  • Create a C file that contains functions in your library.
  • Create a header file for the library.
  • Compile library files:

For example: gcc -c libmy.c -o libmy.o         

  • Create static library. This step is to bundle multiple object files in one static library.

ar rc libmy.a libmy.o         

  • Ready for your use.

How to use them?

In the case of static libraries you have to declare them in the header using "<" ">".

Example:

In this case the libraries "stdio.h", "ctype.h" and "stdlib.h" they are static libraries.

#include <stdio.h
#include <ctype.h>
#include <stdlib.h>
/**
 * main - Program that adds positive numbers
 * @argc: Length of argv
 * @argv: Pointer with the number of elements
 * Return: Return
 */
int main(int argc, char *argv[])
{
	int i;
	int c;
	int sum = 0;
	

	for (i = 1; i < argc; i++)
	{
		for (c = 0; argv[i][c] != '\0'; c++)
	    {
        	if (!isdigit(argv[i][c]))
  			{
  				printf("Error\n");
  				return (1);
  			}
  		}
  	    sum += atoi(argv[i]);
  	}
  	printf("%d\n", sum);
  	return (0);
  	}        

To view or add a comment, sign in

More articles by Axel Bouvier

  • 🚀 OpenAI lanza sus primeros modelos Open-Weight

    Modelos disponibles: GPT‑OSS 120B 117 mil millones de parámetros totales, con solo 5.1 mil millones “activos” en cada…

  • Agentes IA

    Durante los últimos años, el término "inteligencia artificial” se ha popularizado al punto de volverse un lugar común…

  • 🚀✨ ¡Travel Web Page - DIW 1! ✨🚀

    Acabo de publicar VIAJAUY, una página web de viajes que desarrollé para un obligatorio de la materia Diseño de Interfaz…

    1 Comment
  • IoT - Internet of Things

    What is IoT? The Internet of Things (IoT) is a network of physical objects that are fitted with sensors, software and…

  • Recursion

    What is recursion? Recursion is a process by which a function calls itself directly or indirectly. The corresponding…

  • Python3: Mutable, Immutable... everything is object!

    In this blog I will introduce concepts about objects (an object is simply a collection of data (variables) and methods…

  • What happens when you type "ls -l *.c" in the shell?

    What happens when you type `ls -l *.c` in the shell? This article was made to explain what happens when you run the…

  • Hard and Symbolic Links

    Hard and Symbolic links! What is hard link and symbolic link in linux? Hard links and symbolic links are two different…

  • Steps of Compilation

    Four Steps of Compilation: Preprocessing Compiling Assembly Linking Preprocessing: Is the first step, it´s function is…

Explore content categories