What are Static Libraries in C?









What is the structure of a basic script in C?

In the picture above you can see the components that any program in C requires. If you miss any of these parts, you will get an Error warning that might prevent you from compiling your program successfully. 


These components are a header file, a function prototype, a variable declaration, the body of the program. A good practice is to include comments listing and describing the functions, the variables, and the return. You can recognize a comment because it is placed inside backslashes: 
/**    your comment  */


Libraries and functions

The functions are the most important part of a C program. This is the part of the program that performs a certain task. When a function is user-defined, you have to give the function a name, and define the kind of variables that it will take (These functions are declared in the body of the program. However, there are lots of functions that have been created and shared by other developers and, if you want to use them, you just have to link your program to a library containing the function you want to use without having to create it yourself. You just have to call the function in the body. 



Standard Libraries and User-defined Header Files



There are standard libraries like stdlib.h, stdio.h, or math.h.  A library is a collection of functions that can be freely used by anyone. It's a file that ends in ".h". The way we call this library is using #include <stdio.h>. This call is placed at the top of the program. When you are calling a standard library you use <>.

When you use these libraries you can call a function in the body of your program and, when it is compiled, the compiler includes the functions stored in this library in the executable file.




Thus, for compiling a program using this kind of library you need to have a header file (library.h), a function.c file, and a main.c. During the compilation, you need to run this command: 

gcc function.c main.c library.h -o function_exe


In this command, function_exe is the name of the executable file that you get after the compilation process.

Another way to reutiliza functions that you have created before in a new programs it to create your own header files. This file includes the prototype of your function and a file that includes the function that you created. In your program, it needs to be called using #include "mylibrary.h". User-defined functions allow developers to reutilize of code they have developed in the past and write shorter programs by calling functions instead of including them in the program.

Notice that header files only contain the prototype of the function but not the actual function so in the compilation process you have to include both (function.c and library.h). So when you are writing a really long program with lots of functions, the compilation command can get too long and messy and you are more likely to make a mistake or omit one of the functions making your compilation process fail.

A solution to this problem is creating a library. That way you will store the function files and the header file and compile them to create a static library and when you want to use those functions you don't have to include a lot of functions in your compilation command but just the library file.


Static Libraries


So you have created a really long list of functions that you want to start reutilizing in your new programs. You are ready to create your own library. Let's see how they are created and used.

First of all you need to compile the files that will be part of the library into object files (with extension .o).


gcc -c function.c -o function.o



Then, you can create the library by using this command:



ar rcs libraryname.a function1.o function2.o function3.o


the r flag means that any object file that already existed will be replaced, c means that the library will be created and s will create an index file with the object files


If you didn't create this index you can create it later using the command ranlib.


If you want to use a library in a program, this is the command you need to use:


gcc programname.c -L libraryname.a -o main


-L is the flag that indicates that you want to link a library and main is the resulting executable file.







Comments

Popular Posts