A static library is a collection of reusable code that can be shared and reused. A library is usually in form of an archive (ends with the .a
extension).
Remember the stages of compilation
-
Preprocessing
-
Compilation
-
Assembly
-
Linking
An object file is a file that has not gone through the last stage of compilation - linking.
To create object files we need to invoke gcc
with the -c
option.
$ ls
add.c
subtract.c
$ gcc -c add.c subtract.c
$ ls
add.c
add.o
subtract.c
subtract.o
object files usually end with
.o
ar
is the command we use to create libraries. We need to tell ar
the name of the library we want to create.
Let's call our library "calculate" 😎.
$ ar -rc libcalculate.a add.o subtract.o
$ ls
add.c
add.o
libcalculate.a
subtract.c
subtract.o
libraries usually start with
lib
end with.a
, hencelibcalculate.a
Well, it tells ar
to create a new libcalculate.h
library if it doesn't exist, but if it does, it should recreate the library.
This step is optional but pretty useful. When you index a library, you generate an index containing all the cool stuffs in the library. Which makes linking very fast!
by using a command called ranlib
like this
$ ranlib libcalculate.h
But first you need to do on little thing.
*Create an header file containing the prototypes of all the functions in your library, easy!.
Inside our calcute.h
header file...
int add(int a, int b);
int subtract(int a, int b);
Pheew!, finally.
#include "calculate.h"
#include <stdio.h>
int main(void)
{
int a = 5;
int b = 3;
int result;
result = add(a, b);
printf("%d + %d = %d\n", a, b, result);
result = subtract(a, 3);
printf("%d - %d = %d\n", a, b, result);
return (0);
}
Easy! You just need to tell gcc
where to find your library and the name of you library.
$ gcc main.c -L . -l calculate
The
-L
option tells gcc where to find your library, and we supplied.
which means your current working directoryThe
-l
option tells gcc the name of the library without thelib
prefix and the.a
suffix
$ ./a.out
5 + 3 = 8
5 - 3 = 2
Create the static library libmy.a
containing all the functions listed below:
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);
Create a script called create_static_lib.sh
that creates a static library called liball.a
from all the .c
files that are in the current directory.