(not this kind of library)

C Libraries

Mikaela Gurney
4 min readFeb 11, 2018

--

C libraries and header files work in tandem to maintain the functionality, organization, and brevity of large programs.

Developers often take advantage of pre-written code to automate frequently implemented processes within their projects, thereby speeding up development.

An example:

Here, there is no need to explicitly define the printf function. It just works. printf may be called multiple times within any program, provided that the stdio.h header file is appended to the top of the source file prior to compilation.

How does this work?

In order to use a function that is not defined within the source code, header files (ending in .h) provide the compiler with the needed declarations of any functions that are defined within a particular library.

In the example above, printf may be used without provoking a compiler error because the function is declared in the stdio.h file, and is defined within the stdio library. During compilation, library data and object files are linked, rendering the function usable in the resulting executable program.

But what are C libraries?

C libraries are archive files that provide the compiler with a centralized and organized (indexed) collection of object files, which in turn contain methods.

There are two types of libraries: static and dynamic.

These libraries are differentiated by the specific manner in which each links to the executable file.

Static Libraries

At build-time, object files in static libraries are appended to the object code via the static linker. Statically-linked libraries play no role in run-time, as the new executable file will contain a copy of all of the code needed to run.

Appending a library to the executable file results in faster program execution , however, this method notably increases file size and space the executable occupies in memory.

In addition, if any changes are made to the original static library, these changes would not be automatically reflected in copies that are integrated into the executable files.

In order to update a file with the library modifications, it must be recompiled with the updated library.

Dynamic Libraries

Unlike a statically-linked library, a dynamically-linked library is referenced at build time, but not used until at run-time.

When a program executes, the run-time linker saves the memory addresses of the needed functions from the dynamic libraries to memory. The linker then attaches the memory addresses to the executable file.

In the example above, stdio.h references the C standard library.

Although the linking process is slower, it results in a smaller executable file size, which occupies significantly less space in memory.

As all files linked to dynamic libraries contain a reference to same data source, whenever any changes are made to the library, each file linked to it will have access these changes without the need to recompile.

How to Create a Static Library

Creating libraries in three steps:

1. ar

To create a static library, use the command ar.

ar rc libmynumslib.a countnums.o subtractnums.o multnums.o

The 'c' flag signals to ar to create a library if it doesn't already exist. The 'r' flag tells it to update or replace any old object files.

You must add the prefix liband the suffix .a to the library name, and any object files you wish to add to the library (which can be created when compiling your .c files using gcc -c)

2. Indexing

Indexing a library helps speed up the compiler’s search for symbols. The commandranlib is used to create or update the library’s index:

ranlib libmynumslib.a

Caution: compilers complain if the existing index is older than the archive file. To correct his, rerun ranlibto regenerate the library index.

3: Add library name to linker.

Now, we link the library to our object files to create our single executable program.

We use the -l flag for this:

gcc main.o -L. -lmynumslib -o myprogram

Here, our object file main.o is linked with our mynumslib library, which will result the executable program myprogram. Note the removal of the prefixes and suffixes from the lib name.

The -L flag tells the linker where the lib might be located in addition to the standard locations.

--

--