Understanding C static libraries

Understanding C static libraries

First of all it is important to define what we mean by libraries in the programming language C. The Standard C Library, also known as the ISO C Library, is a collection of macros, types and functions for tasks such as input/output processing, string handling, memory management, mathematical calculations and many other operating system services. It is specified in the C standard (e.g. C11). The content is distributed under different headings. Typically, a C library comes in two parts:

1) A header file that defines the functionality that the library is exposing (offering) to the programs that use it.

2) A precompiled binary containing the implementation of that precompiled machine language functionality.

For example, if you want to use the printf() function, the header file <stdio.h> must be included as shown below:

#include <stdio.h>

int main()

{

int x= 10,

printf(“%d\n”, x);

return (0);

}

It is important to mention that libraries are pre-compiled for several reasons. First, since libraries rarely change, it is not necessary to recompile them often. It would be a waste of time to recompile the library every time you write a program that uses them. Second, because precompiled objects are in machine language, it prevents people from accessing or changing the source code, which is important for companies or people who do not want their source code to be available for intellectual property reasons.

However, there are two types of libraries: static libraries and dynamic libraries. In this article, we will focus on static libraries. But what are they? A static library (also known as a file) consists of routines that are compiled and linked directly into your program. When you compile a program that uses a static library, all the functionality of the static library your program uses becomes part of your executable.

In Windows, static libraries usually have a .lib extension, while in linux, static libraries usually have an .a (file) extension.


One advantage of static libraries is that you only have to distribute the executable so that users can run your program. Since the library becomes part of your program, this ensures that the correct version of the library is always used with your program. Also, since static libraries become part of your program, you can use them like the functions you have written for your own program. The disadvantage is that, since a copy of the library becomes part of every executable that uses it, a lot of space can be wasted. Static libraries cannot be easily updated either: to update the library, the entire executable must be replaced.


What is the static link?

Static linking is the process of copying all the library modules used in the program into the final executable image. This is done by the linker and is done as the last step of the compilation process. The linker combines the library routines with the program code to solve the external references and generate an executable image suitable for loading into memory.

Let’s see static linking with an example. Here, we will take a very simple example of the multiplication of two numbers to demonstrate the static linking process. We will develop a multiplication module and place it in a separate mul.c file. The prototype addition module will be placed in a separate file called mul.h. The result_mul.c code file will be created to demonstrate the linking process.

To begin, create a mul.h header file and insert the signature of the add function into it as follows:

int mul(int, int);

Now, create another source code file as result_mul.c, and insert the following code into it.

#include “mul.h”

#include <stdio.h>

int main()

{

int first_num= 12, second_num = 30;

printf(“\n%d * %d = %d\n”, first_num, second_num, mul(first_num, second_num));

return (0);


}


Create one more file called add.c that contains the add module code. Insert the following code in mul.c

int mul(int num_1, int num_2)

{

return(num_1 * num_2);


}

After you have created the above files, you can start building the executable as follows:

gcc -Wall -pedantic -Werror -Wextra -c mul.c

We use the flag -Wall -pedantic -Werror -Wextra to detect if there is any problem during the complication. The above command will produce a binary object file mul.o.

No hay texto alternativo para esta imagen

Now, we have the binary object file, mul.o. After that we have to create a static library by collecting both files together. This will make the job of creating the final executable object file easier and next time you will not have to specify two object files together with mul to generate the final executable object file. Create the static libmulnum library by running the following command:

ar -rcs libmulnum.a mul.o

No hay texto alternativo para esta imagen

The above command produces a new file libmulnum.a, which is a static library containing an object file that can be used later as and when we want to use multiplication functions in any program.

Now with the ranlib function we add each of the headers in our library by executing the following command:

ranlib libmulnum.a

Finally we complicate our library in a new file called mul_result:

gcc result_mul.c -L. -lmulnum -o mul_result

No hay texto alternativo para esta imagen

We run our new file:

./mul_result

No hay texto alternativo para esta imagen

In conclusion, there are many advantages to using the C library functions basically because they are already working code, i.e. they have been subjected to multiple rigorous tests and are easy to use. On the other hand, they are optimized for performance since there is a group of developers who are increasingly improving these codes to be more efficient code optimized for maximum performance, thus saving a lot of time in the development of programming jobs.

RESOURCES

https://www.gnu.org/software/libc/manual/pdf/libc.pdf

https://www.programiz.com/c-programming/library-function


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

Carlos Junior Barros Amador的更多文章

社区洞察

其他会员也浏览了