The Secrets of Static & Dynamic Libraries in C

The Secrets of Static & Dynamic Libraries in C

Why Use Libraries And How Do They Work?

In the C programming language, a library is a single archive file composed of multiple object files. This library archive is applied either during runtime (if dynamic) or the linking phase of compilation (if static), allowing the main program to access whatever functions the library contains.

One benefit of this is that it allows for more memory-and-cpu-efficient usage of resources during the compilation phase. Static library files are generally indexed which makes the looking up of functions and other symbols much faster than just linking multiple object files together each time.

Individual instances of dynamic libraries, on the other hand, can be shared by multiple applications, and thus have much smaller memory overhead. Also, dynamic libraries can be changed or switched without having to recompile and reship the entire application. In this case the main application is decoupled from the library code. These advantages generally tend to outweigh the runtime function lookup costs, but not always. The tradeoff for the dynamic lookup is that it takes processing time. In cases of peak performance it might not be feasible to spare.

But more importantly, libraries save the developer's time, and thus money. By linking libraries, code can be easily and quickly referenced and reused across multiple projects. Also one programmer can easily use the library of another in a compact and portable way. The good usage of libraries will make code more maintainable and error-free, as functionality will be centralized and portable in library archives.

Creating a Dynamic (Shared) Library

Creating a static library is a fairly straightforward affair. One must first compile the C source files into object files via a compiler like GCC with the right flags via a command as:

gcc -fPIC -c *.c

The -fPIC flag tells the compiler to produce Position Independent Code so that it can work as a dynamic library. The -c tells the compiler to stop after the compilation phase leaving the .o files for the next step:

gcc -shared -o libshared.so *.o

This tells gcc to create a -shared library from all the .o files in the current directory with the -o flag indicating that the output file should be named libshared.so. It is a necessary convention to prefix the library name with "lib".

This library must be placed in an appropriate path for it to be linkable at runtime. This is done by setting the environmental variable LD_LIBRARY_PATH. For example to add the present working directory to the library path one might export:

export LD_LIBRARY_PATH=$PWD:$LD_LIBRARY_PATH
 
  

And that's all that is required in order to use the library at runtime! When compiling a program main.c dependent on this shared library named libshared.so one must use gcc as follow:

gcc -L. -lshared main.c

The -L flags indicates where to look for the library. Presently the dot . is used to indicated the current directory. The -l flag contains the name of the shared library with the "lib" prefix omitted. Thus only "shared" in this case since our library is "libshared.so"

Creating A Static Library

Creating a static library is also quite simple. One must first compile the C source files into object files via a compiler like GCC. Once the .o files are available this command will create the archive:

ar -rc libmystaticlib.o *.o

Where libmystaticlib.o is the chosen name of the library and *.o selects all the .o files in the current directory. Both parameters can be easily customized of course. The ar itself is for creating archives. The -c flag creates the archive if it does not exist, and the -r flag replaces any of the component files should they be updated.

Putting Your Static Library To Use

The static library is now created! But to make it even more usable, we should index it in order to speed up that aforementioned symbol lookup and increase compilation efficiency. This is accomplished by the ranlib command, which creates an index of an archive and inserts it into the file:

ranlib libmystaticlib.o

And viola, our static library is ready to be used in compilation via:

gcc myfile.c -L. -lmystaticlib

The -L flags tells the compiler in which directory to search for the library file, in addition to standard library paths. Presently set to the current directory. The -l flag names the library. Note that the "lib" is omitted because it is expected. All static library files should start with "lib." And you're ready to cook with gas!

To view or add a comment, sign in

Explore content categories