Linux System Programming: Creating and Using Static Libraries

Click the above“Mechanical and Electronic Engineering Technology” to follow us
In Linux system programming, a Static Library is a compiled code library that contains a collection of multiple compilation units (such as functions and data) that can be shared by multiple programs. Static libraries typically have the file extension .a.

Creating a Static Library

Compile Source Files: First, you need to write source code and compile it into object files (Object Files), usually with the .o extension.
gcc -c source_file.c
Here, the -c option tells the compiler to compile the source file only, without linking.
Create Static Library: Use the ar tool to package all object files into a static library.
ar rcs liblibrary_name.a object_file1.o object_file2.o ...
  • r: Insert files, create if the static library does not exist.

  • c: Do not issue warnings when creating or inserting files.

  • s: Create an index for reading individual files from the library.

Using a Static Library

Linking at Compile Time: When compiling a program, you need to specify the path to the static library and the object files you want to use from the library.
gcc program.c -L/path/to/library -llibrary_name -o program

    -L: Specifies the search path for the static library.

    -l: Specifies the library to link, without the prefix lib and suffix .a.

Specifying Object Files in the Library: If the static library contains multiple object files, you may need to specify the specific object files you want to link at compile time.

gcc program.c -L/path/to/library -l:object_file_name.o -o program
Setting Runtime Library Path: If the code contained in the static library depends on specific library files, you may also need to set the LD_LIBRARY_PATH environment variable at runtime so that the program can find these libraries.
export LD_LIBRARY_PATH=/path/to/library:$LD_LIBRARY_PATH

Notes

  • Static libraries are integrated into the final executable file at compile time, so the size of the executable file will increase.

  • The code in the static library is copied into the executable file at compile time, so if multiple programs use the same static library, each program will contain a copy of the code, which may lead to memory waste.

  • Programs using static libraries do not need the static library files at runtime since all the required code is already included in the executable file.

A Simple Example to Illustrate the Creation and Use of Static Libraries:

Suppose we have a simple math library that provides two functions: add and multiply.
Write Source Code: First, we create two source files add.c and multiply.c.

add.c:

int add(int a, int b) {    return a + b;}

multiply.c:

int multiply(int a, int b) {    return a * b;}
Compile Source Files: Use gcc to compile these source files into object files.
gcc -c add.c -o add.o
gcc -c multiply.c -o multiply.o

Create Static Library: Use the ar tool to package the object files into a static library.
ar rcs libmath.a add.o multiply.o
Here, libmath.a is the name of the static library we created.
Now, we create a program main.c that uses our math library.
Write Program Using Library: main.c uses the add and multiply functions.

main.c:

#include <stdio.h>
int add(int, int);
int multiply(int, int);
int main() {    int sum = add(3, 4);    int product = multiply(3, 4);
    printf("Sum: %d\n", sum);    printf("Product: %d\n", product);
    return 0;}
Compile Program: When compiling <code>main.c, we need to specify the path and name of the static library.
gcc main.c -L. -lmath -o program
  • -L. specifies the search path for the static library (current directory).

  • -lmath specifies the library to link, which is math in libmath.a.

Run Program: After compilation, you can run the generated program.

./program

The output should be:

Sum: 7Product: 12

This simple example demonstrates how to create a static library and use it in another program. Remember, static libraries are integrated into the executable file at compile time, so you need to specify the path and name of the library each time you compile a program that uses it.

Linux System Programming: Creating and Using Static Libraries

Want to know more

Hurry up and scan the code to follow

Leave a Comment