Static library in C

Static library in C

A library is a collection of code routines (functions, classes, variables, and so on) that can be called upon when building our program, so instead of writing it ourselves, we can go and get it from something that has already been written and optimized. That is where the idea behind libraries comes from.?Basically, we have two kinds of libraries:

  1. static libraries
  2. shared (or dynamic) libraries

The main reason for writing a library is to allow code reusability, thus save considerable development time. A static library is a file containing a collection of object files (*.o) that are linked into the program during the linking phase of compilation and are not relevant during runtime. When a program is compiled, the compiler generates an object file from a source file. After generating the object file, the compiler also invokes the Linker. The role of the linker, in this case, is to copy the code of the library to our object file.

Basically, static libraries are just a collection of object files that are merged by the linker with another object file to form a final executable.

Conventionally, they start with “lib” and end with “.a” or “.lib” (depending on your platform).

To create a static library, we need to specify to the compiler, which is GCC in our case, that we want to compile all library codes (*.c) into object files (*.o) without linking. To do that we are going to use the command below.

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

Flags description:

-c: Compile and assemble, but do not link.

-Wall, -Werro and -Wextra: These aren’t necessary but they are recommended to generate better code.

Note that the "*.c" matches all files in the current working directory with the ".c" extension.

Once we have object file(s), we can now bundle all object files into one static library. To create a static library or to add additional object files to an existing static library, we have to use the GNU?ar?(archiver) program. We can use a command like this:

$ ar -rc libname.a *.o        

This command creates a static library named "libname.a" and puts copies of the object files "add.o" and "mul.o" in it. The 'c' flag tells ar to create the library if it doesn't already exist. The 'r' flag tells it to insert object files or replace existing object files in the library, with the new object files.

After an archive is created or modified, there is a need to index it. This index is later used by the compiler to speed up symbol-lookup inside the library and to make sure that the order of the symbols in the library will not matter during compilation. There are two ways to create or update the index. The first one is, by using the command ranlib, or by adding an extra flag (-s) to the ar command and it becomes like this:

$ ar -rcs libname.a *.o        

In order to list the names of the object files in our library, we can use the ar command with -t flag. Now we can use the command below to create our final executable program:

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

This will create a program using the object file "main.o", and any symbols it requires from the "name" static library.

Flags description:

-L?: Specifies the path to the given libraries ('.' referring to the current directory)

-l?: Specifies the library name without the "lib" prefix and the ".a" suffix, because the linker attaches these parts back to the name of the library to create a name of a file to look for.

All we have to do now is to run our program.

Hopefully, you found this useful. In the next blog post, I will cover another useful topic so stay tuned.

要查看或添加评论,请登录

Eno Gupe的更多文章

  • Mutable, Immutable... everything is object!

    Mutable, Immutable... everything is object!

    Let's suppose that you want to change something on your body. You can change color of your hair, quantity or style of…

  • Class and instance attributes

    Class and instance attributes

    Hello my readers! I would like to thank you upfront for your time while reading this new article. Today I will write…

  • How to list files in a very efficient way?!

    How to list files in a very efficient way?!

    What does the ls *.c instruction do at the conceptual level? (regardless of the language used) Lists the files in the…

    1 条评论

社区洞察

其他会员也浏览了