Learning About Static Libraries In C Programming

I started learning the C programming language around March this year at the ALX internship. It has been a bittersweet experience.

In the sixth week or so, we learned about static libraries. I had difficulties at some point but I finally figured it out.

We were asked to create a static library libmy.a containing functions we had written previously in the internship. Also to create a script called create_static_lib.sh that creates a static library called liball.a from all the .c files. I also changed the file permission, I made the file executable using this command chmod u+x <filename>.

What is a static library?

Libraries are chunks of reusable code. That is they can be used in other projects without creating the functions or writing the code afresh.

A static library is simply a compiled file converted to an object file containing everything such as the functions, header files, variables, libraries etc. required by the main program to operate. For a static library to work only the executable file is needed.

Now going down the path of C compilation where code written is converted to executables. Let's look at the processes involved;

Preprocessor

Firstly, we have the preprocessor which contains the source code. It removes comments and includes the header files in the source code and replaces the macro name with code.

Compiler

The compiler simply takes the file and generates an assembly code.

Assembler

It converts the assembly code into an object code (machine language).

Linker

It uses functions from the library to create an executable file. Here you get the static library (.lib, .a)

A simpler way to put it. After writing your code, you compile it using a compiler to get an executable file or a library.

How I created a static library in C

As earlier mentioned, we were asked to create a static library libmy.a

  • The first thing I did was to write out the functions we were asked to create.

  • Then I created a main.h file containing all the prototypes in the function.

  • The third thing I did was compile the code using gcc I wrote the following command on my terminal;

$ gcc -c -Wall -Werror -Wextra *.c

This was where I got an error message saying error: unused parameter.. underlining out three functions in my code, basically because I set them as an empty file. I had to compile without the above-listed flags. I simply ran the following command;

$ gcc -c *.c

  • After all the .c extension files are compiled. They were converted to object files (.o) still retaining the .c files.

N/B: Another thing to note is that the compilation also helps in detecting bugs in your code, if any bug is found the file won't compile.

Creating the library

To create the library I used the following command;

$ ar -rcs libmy.a *.o

ar - archiver. It's a UNIX utility that maintains a group of files as a single archive file.

the command is simply telling ar to create an achieve (option c) and to insert the objects, replacing older files where needed (option r).

The next command I ran was;

$ ranlib libmy.a

Here is a sample of how it looks like

image.png

After running the ranlib libmy.a command the libmy.a file was automatically created.

To list the symbols from the object file the command $ nm is used. It lists each symbol's symbol value, symbol name and type from object files.

$ nm libmy.a

Using the library

Here we are going to create a random program that uses this library. Firstly, to use the static library in your program, you have to invoke it as part of the linking and compilation process when creating the executable.

  • Lets assume we created a file main.c and some function was added.

Example:

#include <stdio.h>
/**
 * main - main block
 * Return: 0
 */
int main(void)
{
    printf("with proper grammar, but the outcome is a piece of art,\n");
    return (0);
}
  • The next thing is to compile the program.

  • Thirdly, link the compiled program to the library we created using this command; $ gcc main.c -L. -ltest -o main

Note the following:

  • L specifies the path to the library. It looks in the directory for library files.

  • .(the dot after 'L') represents the current working directory.

  • -l option is passed directly to the linker by gcc. Then the linker looks for a standard list of directories for the library

  • test: is the library name.

To run the executable program 'main' use this command;

$ ./main

With this you should be able to compile a program and create a static library in C.

Thank you for reading!

Reference: Medium, Wikipedia, Holberton school resources.