C static libraries

sumin
3 min readJul 16, 2018

--

Unsplash

What is a C static library and why use?

When we compile a C program, the compiler involves four steps which are preprocessing, compilation, assemble, and linking to convert our C program into an executable code. Want to know more about the four steps of compilation process ? click this to see my post . In step four (linking), the object code is combined with required supporting code (system function, libraries and etc) to make an executable program. This step typically involves adding in any libraries that are required. There are two type of libraries which are static and Dynamic library (also called shared library), but I will talk about static library only in this article.

A static library is basically an archive of object files (a file with .o extension) collected into one file (usually with .a extension in Linux and .lib extension in Windows). Since static library is indexed, it can easily search function (printf(), isdigit(), and ect) and variable symbols in them. It is a part for linker to make functions available to your program. Once the symbol is found, the code of library function will copy to your object code. That is why linking a program whose object files are collected in libraries is faster than linking a program whose object files are store separately on disk. Also, by using library, we can reuse our functions instead of copying them every time.

How to get object file?

we can use this command to run:

$gcc -c filename.c

-c : Compile or assemble the source files, but do not link. man page

If you have more than one c files and don’t want to compile that so many time, try this:

$gcc -c *.c

where *.c means all files with .c extension (normally all C files end with .c)

How to create?

The archiver, also know simply as ar, is basic tool used to create static libraries. After we get the object files by following the instruction above, we can use this command to create a static library.

$ar rc libraryname.a object_file1.o object_file2.o

ar: the command to create a static library.

r: tells it to replace older object files in library, with the new one.

c: tells ar to create the library if it doesn’t already exist.

If you have more than one object files and don’t want to compile that so many time, try this:

$ar rc libraryname.a *.o

where *.o means all files with .o extension (normally all output files end with .o)

Already created? how to use?

once we have our library and a main c file, we can use this command to run our code.

$gcc main.c -L. -llibraryname -o main

  • -l<library name without extension>
  • -L tells the linker that libraries might be found in the given directory (‘.’, refering to the current directory)

Then, we just need to type this:

$./main

to get the output.

If we have new or modified archives, we have to index them. To do this, try:

$ranlib libraryname.a

If we want to see the contents of our library, we can use this command:

$ar -t libraryname.a

  • -t : display a table list the contents of archive.

If we want to see the symbols in that library that we just create, we can try this:

$nm libraryname.a

where nm means list symbols from object files.

--

--