Member-only story

What are Static Libraries?

Carlos Esquivel
2 min readOct 11, 2020

Compilers provide a useful option to programers that make coding easier and less redundant. Static libraries are a useful option in c that contain object files which are files that contain some code block within them. This library is usually used when you link two files together during the compiling process. This file is usually indexed meaning that the order and the storage of object files are easier to find which in turn makes for faster linking.

Static libraries are libraries that contain object files that are linked during the compiler process. Since it is a static library it has no effect on the executable file.

To create a static library you want to use the program called ‘ar’ which stands for archive. You then must use some flags that you wish to use in the case above we used ‘r’ ‘c’. The lilbutil.a is the library or in the case of static libraries ‘archive’ that we created. The next three object files are copied and then put inside the ‘archive’ file. In the case of the ‘r’ ‘c’ flags the ‘r’ tells the library to replace older objects with newer ones and the ‘c’ flag tells the archive file to create a new library if it does not already exist.

ar rc libutil.a util_file.o util_net.o util_math.o

After you complete this you want to index your file. This process which is done with the following command.

ranlib libutil.a

This helps the compiler speed up the process of symbol-lookup inside of the library. To use these static libraries all you have to do is make a program along with a header file. Then include the -L flag to link your file with along with the object file. The code below is how you would do it.

gcc main.o -L. -lutil -o prog

--

--

No responses yet