C static libraries đ€
Static libraries are collections of object files that are linked when a file is compiled into an executable file.
Why use libraries?
In a static library, the linker includes the object code of the functions of the library that uses its source code. If you create a static library called lib.a in your source code and then call the functions defined in lib.a, the implementation of those functions would be passed to object code and then included in the final executable file by a launcher.
How they work?
How to create them?
- Listing the files with their respective functions .c
2. compiles files where .o are created
gcc -Wall -pedantic -Werror -Wextra -c *.c
- gcc = integrated compiler, is able to receive a source program and generate a binary executable program.
- Wall -Wextra -pendantic = flags are all warning and error flags that should be included when compiling your code
- -c = link the files to be created as objects
3. Creation of the bookstore with ar
ar -rc libholberton.a *.o
- ar = This program can be used to create static libraries (which are actually archive files).
- -r = replaces older files
- -c = create the library if it doesnât exist
How to use them?
The way we can visualize it is by means of nm
nm libholberton.a
- -nm = lists the value of each symbol, the symbol type and the symbol name of the object files.
We need to index our created file, this will help the compiler when searching inside the library this command is ranlib
ranlib libholberton.a
- -t = serves to view the content of our created library
ar -t libholberton.a
Using a âCâ library in a program
the use of the â-Lâ flag â this flag tells the linker that libraries can be found in the given directory (â.â, referring to the current directory), in addition to the standard locations where the compiler searches for system libraries.
gcc 0-isupper.c -L. -lholberton -o isupper
./isupper