static libraries

Khalil Hassayoun
3 min readOct 11, 2020

A static library is a compiled object file containing all symbols needed to operate by the main program (functions, variables, etc.) as opposed to having to bring different entities into the programming language C. Static libraries are not loaded at runtime by the compiler; the executable file just needs to be loaded. Instead, static library object files are loaded during their linking process, near the end of the compilation.

a static library . How does it work ? and why use it ?

The use of a static library ensures that during the linking process, only one object file needs to be pulled in. This compares with having several object files (one for each function etc) pulled by the compiler during linking. The advantage of using a static library is that it is indexed by the functions and other symbols loaded into it. Instead of having to search for each entity on different parts of the disk, this ensures that the program only needs to reference a single archived object (.a) file organized by the entity. The software linking this library will load much more easily as a consequence of providing one ordered object list. Because at the end of the compilation, the static library object (.a) file is connected,and program that needs this library must have its own copy of the related library. A single dynamic shared library, unlike a static library, can be loaded into memory at runtime and accessible to other programs related to the same library.In a future column, more on dynamic libraries.

How to use Linux and Unix-Like OS! to build a static library?

1.Place all the related files in a single directory, including the header file (.h) that contains prototypes for the respective entities. Make sure that the file header (.h) contains the macros # ifndef < HEADERFILE> H and # define < HEADERFILE> H at the top and # endif at the bottom, so that the file header is specified only once instead of every time it is called.

2.Compile all source (.c) files by batch. Use the -c option to make the compiler not link object files, but instead generate a counterpart object file (.o) for each source file (.c).

3.All object (.o) files are archived into one static library (.a) file. Use the -r command option to ensure that it will be replaced if the library (.a) file already exists. The -c command option should be used so that it will be generated if the file does not exist.

4.Shift the library (.a) file to the same directory that resides in the entry-point file.

5.instead of including all the file names in the compilation command, it is only important to reference the library (.a) file.

How to use the static libraries

we use :

int main(void)
{
int x = 5;
int y = 8;
int result; result = sum(x, y);
return (0);
}

this is a common error when using sum() :

gcc my_program.c// oops... ///tmp/ccGLAk66.o: In function `main':my_program.c:(.text+0x26): undefined reference to `sum'collect2: error: ld returned 1 exit status

The programmer is uncertain of what the sum is. We have to tell them to look at our library:

gcc my_program.c -L. -lforme -o my_program

--

--

Khalil Hassayoun
0 Followers

Holberton school student . Future software engineer