Image for post
Image for post

A library is basically a package of items that one can call from their program. Static libraries end with .a. For example, “my_library.a”.

You can create a static library by typing the ‘ar’ command in your command line followed by the file name, and any previously created object files you would like to place in the library. An example of that would be

ar rcs my_library.a file1.o file2.o

This example explains how static libraries are created and how they work.

ubuntu@ip-172-31-63-244:~/holbertonschool$ l
total 44
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 25 02:47 .
drwxrwxr-x 19 ubuntu ubuntu 4096 Mar 25 02:44 ..
-rw-rw-r-- 1 ubuntu ubuntu 89 Mar 25 02:44 0-print_z.c
-rw-rw-r-- 1 ubuntu ubuntu 132 Mar 25 02:44 1-print_alphabet.c
-rw-rw-r-- 1 ubuntu ubuntu 132 Mar 25 02:44 2-print_tebahpla.c
-rw-rw-r-- 1 ubuntu ubuntu 166 Mar 25 02:44 3-print_base16.c
-rw-rw-r-- 1 ubuntu ubuntu 162 Mar 25 02:44 4-positive_or_not.c
-rw-rw-r-- 1 ubuntu ubuntu 636 Mar 25 02:44 5-print_number.c
-rw-rw-r-- 1 ubuntu ubuntu 178 Mar 25 02:47 my_functions.h
-rw-rw-r-- 1 ubuntu ubuntu 76 Mar 25 02:44 print_char.c
-rw-rw-r-- 1 ubuntu ubuntu 1392 Mar 25 02:46 print_char.o
ubuntu@ip-172-31-63-244:~/holbertonschool$ gcc -Wall -pedantic -Werror -Wextra -c *.c
ubuntu@ip-172-31-63-244:~/holbertonschool$ l
total 68
drwxrwxr-x 2 ubuntu ubuntu 4096 Mar 25 02:47 .
drwxrwxr-x 19 ubuntu ubuntu 4096 Mar 25 02:44 ..
-rw-rw-r-- 1 ubuntu ubuntu 89 Mar 25 02:44 0-print_z.c
-rw-rw-r-- 1 ubuntu ubuntu 1400 Mar 25 02:47 0-print_z.o
-rw-rw-r-- 1 ubuntu ubuntu 132 Mar 25 02:44 1-print_alphabet.c
-rw-rw-r-- 1 ubuntu ubuntu 1408 Mar 25 02:47 1-print_alphabet.o
-rw-rw-r-- 1 ubuntu ubuntu 132 Mar 25 02:44 2-print_tebahpla.c
-rw-rw-r-- 1 ubuntu ubuntu 1408 Mar 25 02:47 2-print_tebahpla.o
-rw-rw-r-- 1 ubuntu ubuntu 166 Mar 25 02:44 3-print_base16.c
-rw-rw-r-- 1 ubuntu ubuntu 1464 Mar 25 02:47 3-print_base16.o
-rw-rw-r-- 1 ubuntu ubuntu 162 Mar 25 02:44 4-positive_or_not.c
-rw-rw-r-- 1 ubuntu ubuntu 1472 Mar 25 02:47 4-positive_or_not.o
-rw-rw-r-- 1 ubuntu ubuntu 636 Mar 25 02:44 5-print_number.c
-rw-rw-r-- 1 ubuntu ubuntu 2048 Mar 25 02:47 5-print_number.o
-rw-rw-r-- 1 ubuntu ubuntu 178 Mar 25 02:47 my_functions.h
-rw-rw-r-- 1 ubuntu ubuntu 76 Mar 25 02:44 print_char.c
-rw-rw-r-- 1 ubuntu ubuntu 1392 Mar 25 02:47 print_char.o
ubuntu@ip-172-31-63-244:~/holbertonschool$ ar -rc libholbertonschool.a *.o
ubuntu@ip-172-31-63-244:~/holbertonschool$ ar -t libholbertonschool.a
0-print_z.o
1-print_alphabet.o
2-print_tebahpla.o
3-print_base16.o
4-positive_or_not.o
5-print_number.o
print_char.o
ubuntu@ip-172-31-63-244:~/holbertonschool$ ranlib libholbertonschool.a
ubuntu@ip-172-31-63-244:~/holbertonschool$ cat main.c
void print_alphabet(void);

int main(void)
{
print_alphabet();
return (0);
}
ubuntu@ip-172-31-63-244:~/holbertonschool$ gcc main.c
/tmp/ccQa2r6L.o: In function `main':
main.c:(.text+0x5): undefined reference to `print_alphabet'
collect2: error: ld returned 1 exit status
ubuntu@ip-172-31-63-244:~/holbertonschool$ gcc main.c -L. -lholbertonschool -o alpha
ubuntu@ip-172-31-63-244:~/holbertonschool$ ./alpha
abcdefghijklmnopqrstuvwxyzubuntu
ubuntu@ip-172-31-63-244:~/holbertonschooc

First off, the ‘l’ command is used to list all files in the current directory. Then the gcc -Wall -pedantic -Werror -Wextra -c *.c command is used to compile all of the files that end in .c. * is a wildcard that means all of the files of a certain type. Next, the ‘l’ command is used again to list all of the files in the current directory. Now it includes the .c files as well as the .o files that were created as the .c files were compiled. The ar -rc libholbertonschool.a *.o command creates a static library called libholbertonschool.a and it includes all of the files ending in ‘.o’. ar -t libholbertonschool.a command shows all of the files in the static library. The ranlib libholbertonschool.a command is used to create or update the index of the library. Then a file from the library is complied to make sure the all of the files that need to be in the library are actually in the library.

Why should we use libraries?

Libraries are useful for storing frequently used files so you don’t need to link them to every program that has a use for them. The linker will look for those files in libraries.

How to use static libraries?

We can use static libraries to make the complilation process more efficient.. We can call a function from the main file directly so there is no need to include a helper function. In order to compile a library we use the -l flag followed by the name of the static library. The -L flag tells the linker where to look for the library.

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store