C - Static libraries
- Why use static libraries
Because libraries are a files containing several object files, that can be used as a single entity in a linking phase of a program, that′s mean only the program's executable file is needed in order to run the program.
- How static libraries work
Static libraries are just collections of object files that are linked into the program during the linking phase of compilation.
- How to create static libraries
To create a static library is needed to follow the next step:
First, set collection of funtions (C files) that will belong to the library.
Second, compile with gcc all the files, obtained before, and get the ".o" extension of the ".c" functions using the "-c" flag. This step is to get the assembly code of each ".c" file. (The flags -Wall, -pedantic, -Werror, Wextra will help to convert all warnings in error when the files are compiled)
Now, the assembly files were created.
Third, all assembly files (".o extension") will be passed to the library libholbertonschool.a. To do it, the "ar" program (archiver) will be used accompained by the flags -rc. (The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to replace older object files in the library, with the new object files).
Now the library was created. To see what library has, it will be used the "ar" program with the "-t" flag. (The flag 't' list the files allocated in the library)
Those are the file stored in "libholberton.a" library.
Fourth, it will index, with ranlib program, the library to be later used by the compiler to speed up symbol-lookup inside the library.
- How to use static libraries
To use a static library is necesary compile the main funtion with the library using "-l" flag. ('l' flag add the library's name to the list of object file names given to the linker).
For example:
First, create a main funtion with the funtion′s prototype.
Second, compile the main funtion with library using 'l' flag to create alpha file executable using 'o' flag. ('L.' flag tells the linker that libraries might be found in the given directory)
Third, execute the "alpha" file.
This will be the output of the previusly executed file.
REFERENCE
Guy Keren. (1998-2002). Building And Using Static And Shared "C" Libraries: AS IS . Recuperado de https://docencia.ac.upc.edu/FIB/USO/Bibliografia/unix-c-libraries.html#what_is_a_library.