In C programming, creating a library allows you to encapsulate functions that can be reused across multiple programs. A library typically consists of two parts:
Header file (.h): Contains function declarations (prototypes) and macro definitions.
Source file (.c): Contains the actual implementation of the functions.
1. Write the Header File (.h)
The header file contains function declarations and any macro or type definitions you might need. This file is included in any program that uses the library.
For example, let's create a header file for basic math operations.
mathlib.h
#ifndef MATHLIB_H
#define MATHLIB_H
// Function declarations
int add(int a, int b);
int subtract(int a, int b);
int multiply(int a, int b);
float divide(int a, int b);
#endif // MATHLIB_H
The #ifndef, #define, and #endif are header guards, preventing multiple inclusions of the same header file.
2. Write the Source File (.c)
The source file contains the implementation of the functions declared in the header file.
mathlib.c
#include "mathlib.h"
int add(int a, int b) {
return a + b;
}
int subtract(int a, int b) {
return a - b;
}
int multiply(int a, int b) {
return a * b;
}
float divide(int a, int b) {
if (b != 0) {
return (float) a / b;
} else {
return 0.0; // Handle divide by zero
}
}
This file must be compiled into an object file to create the library.
3. Compile the Library
To create a static library (with the .a extension), compile the source file into an object file and then archive it.
Compile the source file:
gcc -c mathlib.c -o mathlib.o
This command compiles mathlib.c into an object file mathlib.o.
Archive the object file into a library:
ar rcs libmathlib.a mathlib.o
This creates a static library called libmathlib.a.
4. Use the Library in a Program
Now you can create a program that uses your library. In your C program, include the header file and link against the library when compiling.
main.c
#include <stdio.h>
#include "mathlib.h"
int main() {
int x = 10, y = 5;
printf("Add: %d\n", add(x, y));
printf("Subtract: %d\n", subtract(x, y));
printf("Multiply: %d\n", multiply(x, y));
printf("Divide: %.2f\n", divide(x, y));
return 0;
}
To compile this program and link it with the library, use:
gcc main.c -L. -lmathlib -o main
-L. tells the compiler to look in the current directory for libraries.
-lmathlib links the libmathlib.a library (the lib prefix and .a suffix are implicit).
Now, run the program:
./main
5. Shared Library (Optional)
If you want to create a shared library (dynamic linking), you can do the following:
Compile the source file:
gcc -fPIC -c mathlib.c
Create the shared library:
gcc -shared -o libmathlib.so mathlib.o
Compile and link with the shared library:
gcc main.c -L. -lmathlib -o main
Ensure the shared library can be found at runtime:
export LD_LIBRARY_PATH=$PWD:.
Now you have a shared library (.so file) that can be used in your programs.
Header file (.h): Contains function declarations.
Source file (.c): Contains function implementations.
Static library (.a): Archiving object files for static linking.
Shared library (.so): Dynamically linked at runtime.