Static Libraries in C: A brief introduction.

Van Phan
3 min readMar 4, 2019

--

Before introducing static library, we need to know the definition and usage of a library.

What is a Library in programming and How it works?

A library is one of the most important tools which users need in coding. The library is a set of functions or programs which are collected and organized in a file. All of the functions in the library are the objects file, which can achieve through linking stage of compiler process. Therefore, users can easily find and use the functions or variables that they want in the library. Furthermore, using libraries are faster than linking separate files to a program, so it will increase the efficiency of the programs and saving coding time. There are two types of library:

  • A static library is a library that contains all object files connected with the program throughout linking stage process. The special of object files in the static library is that they are not relevant during the runtime, only the program file linked to the library is executed. The static library is very useful but it has a big disadvantage: portability. If we update or change the static library, we need to compile the executed file again, and the static library will take a lot of memory when you use it.
  • The second type of library is a dynamic library or shared library. However, the main topic of this article is the static library, so I will describe the shared library briefly. The object files in the shared library don’ t connect with the program at first, they are only accessed when the program is executed.

How to create a Static library?

Before we create a static library, we need to copy or move all the C files that you want to your current working directory. After that, we need to convert those files to object files by this command in the compile process:

gcc -c *.c

You can use the * wildcard to do it faster or type the object files that you want after c flag. The command will change the .c extension to the .o extension to adapt the standard of the static library. When you have all object files, we will run this command to create the static library:

ar -rc libnameofthelibrary.a *.o

All static libraries will have the .a extension at the end. After you run the command, you have created a static library. To see the content of the library, you can use:

ar -t libnameofthelibrary.a

When you want to update or add an object file to the library, you will use the commandranlib.

ranlib libnameofthelibrary.a

If you want to see the symbol value, symbol name or other symbols in the object files, nm is the command that you want.

nm libnameofthelibrary.a

How to use a static library?

After creating a static library, this is the time that we use it to compile a program.

gcc main.c -L. -lnameofthelibrary -o executed_file_name

The -L flag helps to specify the place that the static library can find in the given path or directories. The -l flag replaces the lib at the beginning and .a extension at the end when we compile the program with the library.

Finally, that is a brief introduction to the static library in C.

--

--