.a
.Creating a Static Library
.o
extension.gcc -c source_file.c
-c
option tells the compiler to compile the source file only, without linking.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
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
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:
add
and multiply
.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;}
gcc
to compile these source files into object files.gcc -c add.c -o add.o
gcc -c multiply.c -o multiply.o
ar
tool to package the object files into a static library.ar rcs libmath.a add.o multiply.o
libmath.a
is the name of the static library we created.main.c
that uses our math 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 ismath
inlibmath.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.

Want to know more
Hurry up and scan the code to follow