DEV Community

Cover image for C Static Libraries
Kristi
Kristi

Posted on

C Static Libraries

What is a static library?

A static library is an archive file containing object files that remain static until the program is recompiled. This static library can be used as a single entity in a linking phase of a program.

Why do we use libraries?

Using a static library means only one object file needs to be pulled in during the linking phase. This speeds up the linking phase because there are fewer files to process. The benefit of using a static library is that the functions and other symbols loaded into it are indexed. For this reason, linking a program whose object files are ordered in libraries is faster than linking a program whose object files are separate on the disk.

How to create them?

First of all, we include all function prototypes of .c files into the header file:

#ifndef MAIN_H
#define MAIN_H

/* all function prototypes */

#endif
Enter fullscreen mode Exit fullscreen mode

Then we compile .c files with gcc -c command. After this process, we will have .c files compiled into object files '.o' which are necessary for the library.

The next step now is to create an archive file(static library) with object files '.o' and for this step, we use the command below:

ar -rc libexample.a *.o
Enter fullscreen mode Exit fullscreen mode
  • ar is a UNIX command for creating and maintaining library archives
  • -r is used to replace or add files to archive
  • -c is used to create a new archive file
  • libexample.a is the name of the file with the extension '.a' which stands for archive
  • And the final step of the command is the selection of all object files

How to use them?

After creating the library (archive file) we use it in a program.

gcc filename.c -L . -lexample -o filename
Enter fullscreen mode Exit fullscreen mode
  • gcc (GNU Compiler Collection) - command to compile C files
  • filename - name of the file
  • -L . - tell the linker that the libraries might be found in the current directory '.'
  • -l - links the C file with the library file
  • example - the name of the static library (check notes at the end of the article)
  • -o is used to change the name of the executable file
  • filename - the name of the executable file

Conclusion

This was a basic explanation of what is a static library, how to create and use them. Hope you find it useful.

Notes

We omitted the "lib" and ".a" when mentioning the library on the link command. The linker attaches these parts back to the name of the library to create a name of the file to look for.

Top comments (1)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Your description of a static library applies equally well to a dynamic library. The actual difference is that the object code in a static library is merged into the binary on disk for the programs that uses it where as a dynamic library isn't and is loaded into memory only when the program is actually run (every time).