C static libraries

C static libraries

what is static libraries?

In the C programming language, a static library is a compiled object file containing all symbols required by the main program to operate (functions, variables etc.) as opposed to having to pull in separate entities.

Why use libraries?

When u use a static library u need just to pull 1 object file during the linking phase also the functions and other symbols loaded into it are indexed .

So basically static libraries are object files that are later combined with another object to form a final executable to make the process faster.

How they work?

When you compile a source file you get an object file. Depending on your platform its extension may be .o or .obj or anything else. A static library is basically a collection of object files, kind of like a .zip file but probably not compressed. The linker, when trying to generate an executable tries to resolve the referenced symbols, i.e. locate in which object file (be it in a library or otherwise) they are defined and links them together. So, a static library may also contain an index of defined symbols in order to facilitate this. Exact implementation depends on the specific linker and library file format but the basic architecture is as mentioned.

How to create them?

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.

The basic tool used to create static libraries is a program called 'ar', for 'archiver'. This program can be used to create static libraries (which are actually archive files), modify object files in the static library, list the names of object files in the library, and so on. In order to create a static library, we can use a command like this:

$ ar -rc mylib.a *.o        

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.

$ ranlib mylib.a        

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.

If we want to see the contents of our library, we can use the ar option -t.

$ ar -t libholberton.a        

How to use them?

We can use it in a program by adding the library's name to the object file(s) given to the linker.


Ref: Stackoverflow, Reddit, Medium, Quora, Dev.to

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

Mahdi Bani的更多文章

社区洞察

其他会员也浏览了