Skip to main content

Overview

Static libraries are collections of compiled object files that are linked into your program at compile time. They provide a way to organize and reuse code across multiple projects without needing to recompile the source files each time.
Static libraries are linked at compile time, becoming part of your executable. This increases the binary size but eliminates runtime dependencies.

What is a Static Library?

A static library is an archive of object files (.o files) created using the ar (archive) utility. In Linux, static libraries conventionally have the .a extension and are named with a lib prefix (e.g., libmy.a).

Advantages

  • Fast execution: No runtime linking overhead
  • Portable: Everything is contained in the executable
  • Version stability: No dependency on external library versions

Disadvantages

  • Larger executables: Library code is copied into each program
  • Updates require recompilation: Changes to the library require relinking all programs

Creating a Static Library

Library Functions

Our static library includes common string and memory manipulation functions:
main.h
int _putchar(char c);
int _islower(int c);
int _isalpha(int c);
int _abs(int n);
int _isupper(int c);
int _isdigit(int c);
int _strlen(char *s);
void _puts(char *s);
char *_strcpy(char *dest, char *src);
int _atoi(char *s);
char *_strcat(char *dest, char *src);
char *_strncat(char *dest, char *src, int n);
char *_strncpy(char *dest, char *src, int n);
int _strcmp(char *s1, char *s2);
char *_memset(char *s, char b, unsigned int n);
char *_memcpy(char *dest, char *src, unsigned int n);
char *_strchr(char *s, char c);
unsigned int _strspn(char *s, char *accept);
char *_strpbrk(char *s, char *accept);
char *_strstr(char *haystack, char *needle);

Example Implementations

#include "main.h"

/**
 * _strlen - length of string function
 * @s: pointer to char
 * Description: length of string
 * Return: length of string
 */

int _strlen(char *s)
{
	int i;
	i = 0;
	while (*s != '\0')
	{
		i++;
		s++;
	}
	return (i);
}

Build Process

1

Compile Source Files to Object Files

Compile each .c file into an object file (.o):
gcc -Wall -pedantic -Werror -Wextra -c *.c
This creates object files like _strlen.o, _memset.o, _strcmp.o, etc.
The -c flag tells gcc to compile without linking, producing object files.
2

Create the Archive

Use the ar command to create the static library:
ar -rc liball.a *.o
Flags explained:
  • r - Replace or add files to the archive
  • c - Create the archive if it doesn’t exist
3

Index the Archive

Create an index for faster linking:
ranlib liball.a
This creates a symbol table index within the archive.

Automated Build Script

The complete build process can be automated with a shell script:
create_static_lib.sh
#!/bin/bash
gcc -Wall -pedantic -Werror -Wextra -c *.c
ar -rc liball.a *.o
ranlib liball.a
Make the script executable with chmod +x create_static_lib.sh before running it.

Using the Static Library

Compilation

To use the library in your program:
gcc main.c -L. -lmy -o output
Flags explained:
  • -L. - Look for libraries in the current directory
  • -lmy - Link against libmy.a (the lib prefix and .a suffix are implicit)

Example Program

main.c
#include "main.h"
#include <stdio.h>

int main(void)
{
    char str[] = "Hello, World!";
    char dest[50];
    int len;

    len = _strlen(str);
    printf("Length: %d\n", len);

    _memset(dest, 'A', 10);
    dest[10] = '\0';
    printf("Memset result: %s\n", dest);

    return (0);
}

Examining Library Contents

List Archive Contents

ar -t libmy.a
This displays all object files in the archive.

Display Symbol Table

nm libmy.a
Shows all symbols (functions, variables) defined in the library.

Detailed Listing

ar -tv libmy.a
Provides detailed information including file sizes and timestamps.

Common Issues and Solutions

If you get undefined reference errors during linking:
  1. Ensure the library is in the library path (-L flag)
  2. Check that you’re linking the correct library (-l flag)
  3. Verify the function exists in the library with nm
nm libmy.a | grep function_name
The order matters when compiling with static libraries:
# Correct
gcc main.c -L. -lmy -o output

# May cause issues
gcc -L. -lmy main.c -o output
Object files and source files should generally come before library flags.
This occurs when the same function is defined in multiple object files:
  1. Check for duplicate implementations
  2. Ensure each function has only one definition
  3. Use nm to identify where symbols are defined

Best Practices

Naming Convention

Always prefix library names with lib and use .a extension:
  • libmy.a
  • libutils.a
  • libmath.a

Header Files

Provide header files (.h) with function declarations for library users.

Documentation

Document each function’s purpose, parameters, and return values in comments.

Testing

Test library functions thoroughly before distribution.

Build docs developers (and LLMs) love