Dynamic Libraries!

Ashley Price
2 min readDec 15, 2020

A library is a file containing several object files, that can be used as a single entity in a linking phase of a program. Linking a program whose object files are ordered in libraries is faster than linking a program when their object files separate on the disk, and when you have fewer files to look for and open it further speeds up the program.

Static libraries are a collection of object files put together into an archive by the compiler. The linker will search the library for object files that provide any of the missing symbols in the main program and pull them in for linking.

In contrast, a dynamic library consists of separate files containing separate pieces of object code. These files are dynamically linked together to form a single piece of object code. Dynamic libraries contain extra information that the operating system will need to link the library to other programs.

How to Create a Dynamic Library (Linux)

To create a dynamic library, there are a few simple steps. The first command you will need to use is gcc *.c -c -fPIC. gcc calls the compiler and tells it to compile all .c files in the directory. -c flag prevents the object files from linking at this stage. -fPIC makes sure the code is position dependant, meaning the computer can load the code to memory at any point.

The next step is gcc *.o -shared -o liball.so. Once again you are using gcc to compile, and this time calling the object files. -shared flag tells the compiler to compile the object files and put them into a dynamic library. Dynamic libraries must start with “lib” and end with the extension .so. You can substitute your library name where the example used here is “all”.

How to Use a Dynamic Library (Linux)

--

--