C static libraries

C static libraries

A static library which is a statically linked library is a set of routines, external functions, and variables that are resolved in a caller called compile-time and copied into a target application by a compiler. Any static library function can call a function or procedure in another static library. The linker?and loader handle this the same way as for kinds of other?object files. Static library files may be linked at run-time?by a?linking loader.

  1. How do they work?

When in the last stage of the compilation process linker generates an executable file it also adds (copies from the library) the code of a function we are using in our program. That function is part of the static library that we included in our code to be able to use that function. For every function in our code that is from the static library, the linker will include code for it in our executable file. This is an issue if we use a lot of functions in the same program because for every function that we include the size of the program will increase because we are copying code of that function into an executable file.

One other problem with static linking would be portability. So what happens when we move that executable file? Nothing! And that is the problem. If some changes were made to the library or the library got updated in the meantime, those changes will not reflect in our program unless we recompile it with the updated library.

2. Why do we use them?

One must wonder now… Why would we ever use libraries if they increase the size of our program and make us recompile it every time some changes are made to it? But even with these drawbacks of static libraries; if you think about it, what if you move the program and there is no library you need it to be linked with. In this case, the code is already copied into your executable file and you don’t have to worry about it. This at the same time allows easier distribution and installation of your program.

But why do we use them you might ask? Have you ever written the same code over and over again in different programs and you wish there was some magical way to just skip that part? Well, that’s what libraries do. You can add those pieces of code to the library and just include a library in your compilation process and the compiler will insert that code into your program when you want it to, without you having to write it over and over again.

Now let’s see how we make that happen.

3. Creating a static library

The basic tool used to create static libraries is a program called ‘ar’, for ‘archiver’ and this is how it would look like in the command line.

ar rc libname_of_your_library_here.a file1.o file2.o file3.o

This command creates a static library named ‘libname_of_your_library.a’ and puts copies of the object files “file1.o”, “file2.o” and “file3.o” in it. Every?library name?starts with lib and ends with .a extension.

Let’s take a look at some examples.

First, we write some programs to individually perform some tasks we use often and write all their?prototypes?in the?header file. Now to place all the object files into our library, we first need to create our object files from all those .c files and we do that by using gcc to compile them but with option -c which returns the same file names but as object files and with extension .o. Now those .o files are the object files that we want to save in our library (named my_lib in the example) and we do that by using the above-mentioned command?ar?and with the ‘rc’ option so that in case of the library does not exist one gets?created?(c option) and in case it does old files get?replaced?with new ones (r option).

Just to make sure all of our object files were added correctly to our library we can use art to display a table listing the contents of the archive.


ar -t displays a table listing the contents of the archive

Once we create our archive or modify it, we need to index it. And why do we do that? Well just like when you are looking for a specific chapter in a book you look at the table of contents to find it faster. The compiler does the same thing if our archive is indexed. And to index our library (archive) we run this command:

4. How to use the C library

So we created our library and now we want to see it in action so here is how we do that. We do this by adding the library name to the list of object file names given to the linker. And to accomplish this we use a special flag -l attached in front of our library name without lib at the beginning and .a at the end. The linker takes care of this for us. Flag -L. tells the linker that libraries might be found in the given directory. So we are looking at something like this:

gcc main.c -L. -lname_of_your_library_here?-o?your_exe_file_name

This will create a program using object file main.o created from main. c and any functions we used from the specified static library.

Now we know that just by adding our library during the compiling process we can save so much time because static libraries allow us to add to the functions that we don’t want to write over and over again.

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

Tatiana Fernández的更多文章

社区洞察

其他会员也浏览了