Oui oui oui C static libraries

Jimmy Thong
meatandmachines
Published in
4 min readNov 6, 2016

(Note: this article assumes you know about How C compiles code)

Let’s talk about C static libraries.

I. Why do we use them?

II. How do they work? How do we use them?

III. How do we create them?

I. Why do we use C static libraries?

In software engineering, it’s preferred to use smaller units of related (code) files, than it is to use a huge-ass source code file. Why? Nobody likes looking for needles in haystacks, so we practice a separation of concerns.

The library is a compiler-supplied tool combining all the object files into one indexed file. Within this index file, it is organized so symbols are easy-to-find.

There are two kinds of libraries, static & dynamic — unfortunately (and fortunately), static libraries uses dynamic loading, which is slower

  • If a second program linked with the same library is executed, it can use the same copy of the shared library, thus saving a lot of memory. (ex. the standard C library is a shared library, used by all C programs).
  • Only 1 copy of the library is stored in memory at any given time.
  • If we re-compile the dynamic library and try to run a second copy of our program with the new library, we’ll get stuck.

TK II. How do C static libraries work? How do we use them?

In the fourth stage of code compilation, let’s say we call the function “one” held within a library.

We compile the code, which turns into binary machine code, calling the function one().

This is the library in the compiled format

Linker, the fourth stage in the compilation process will take this object code and turn it into an executable file. The code of function one() will be included itself in this executable file.

With static linking, you could call 300 functions with 2 object files.

All that said, there are two disadvantages of static linking.

  1. The size of the executable file will be based on the number of functions held within the object files, which could be potentially huge (300 functions…).
  2. Changes are harder to keep updated. In an example case, an .exe file has been generated, a user has taken a file — but then, changes are made to the library. The user would have to re-compile the .exe file to get these changes to the library — and every time changes are made to the library.

III. How do you create C static libraries?

To create a C static library, the command (for “archiver”):

ar

To create a static library named ‘libutil.a’ & put copies of the object files “util_file.o”, “util_net.o” and “util_math.o” into it.

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

You’ll notice the “rc” flags after “ar” — well, here’s their function:

  • The 'c' flag tells “ar” to create the library if it doesn't already exist.
  • The 'r' flag tells it to replace older object files in the library, with the new object files.

If the library file already exists, it has the object files added to it, or replaced, if they are newer than those inside the library.

The “ar” command can also be used to modify object files in the static library and list the names of object files in the library.

The index is used by the C compiler to do a symbol-lookup. The index speeds up this symbol-lookup process.

To create, or update, said “index” of an example file “libutil.a” —

ranlib libutil.a

Note: When a file’s index generation date is older than a file’s last modification date, the compiler (trying to use the index) will complain it’s out of date and abort. There are two ways to deal with this:

ranlib

…to re-generate index.

OR

Copying the archive file to another location using:

cp-p

The “-p” flag keeps all attributes of the file, including access permissions.

Sources:

Building And Using Static And Shared “C” Libraries by Guy Keren

What is difference between Dynamic and Static library (Static and Dynamic linking)

Wanna learn more about our full-stack software engineering school with no upfront tuition and a project-based learning curriculum? Check us out.

--

--

Jimmy Thong
meatandmachines

Makerspace Teacher, Code Coach, Amateur Home Cook, Writer