Static libraries in C
Ricardo Monta?a
Desarrollador Full Stack | Python | JavaScript | MySQL | HTML | CSS | Angular | ReactJS. Creación de soluciones innovadoras y escalables.
What is a static C library?
Also called object-libraries, they are collections of (compiled) object files grouped into a single file with the extension .lib, .a, etc. along with one or more header files (usually .h).
During the construction of the application, the preprocessor includes the header files in the sources. Subsequently, during the linking phase, the linker includes in the executable the modules corresponding to the library functions and classes that have been used in the program, so that the set becomes part of the executable. Hence its name: Statically Linked Libraries
Leaving considerations of convenience and speed aside, the result of using one of these libraries is no different from what can be obtained by writing the corresponding functions or classes in the source and compiling them as one more module of our application.
Note: compilers generally have specific tools for creating static libraries. For example, the Borland C++ compiler is the executable TLIB.EXE ( 1.4.0w1); the GNU ones are named ar and ranlib. As we will see in the examples, they can also be created using specific options in the build order.
Note: when a static library is created from one or several relocatable files (objects), the process of including this table or dictionary of symbols can be executed in a single step or in two, although always at the moment of creating the library. For example, Boland's tlib creates the library and the table in one process. Instead, GNU ar can create the library and then add the table (the latter can also be done with ranlib). When a new module is added to an existing library, the index is updated by the same tool that adds the content.
?Why use libraries in C
As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is better to put this function or functions in a library to speed up the compilation of the program. Static libraries allow users to link to programs without having to recompile their code, saving compile time.
Static libraries are also useful for developers when they want to allow programmers to link to their library, but don't want to give away the library's source code, which is a benefit to the library vendor, but obviously not a benefit to the library. the programmer trying to use the library.
How do static libraries work in C?
A library is a set of files used to develop software. It is usually composed of code and data, and its purpose is to be used by other programs in a completely autonomous way. Plain and simple, it's an importable file.
How to create static libraries in C?
?Once we have our code, to get a static library we must perform the following steps:
?Obtain the object files (.o) of all our sources (.c). To do this, they are compiled with cc -c source.c -o source.o. The -c option tells the compiler not to create an executable, just an object file. Here I put the cc compiler, because it is the one I have used for the example, but you can use gcc, or g++ (for C++) or one of fortran, pascal, etc.
Create the library (.a). For this, the ar command is used with the following parameters: ar -rv libname.a source1.o source2.o ... The -r option tells the ar command that it has to insert (or replace if they are already inside) the files object in the library. The -v option is "verbose", so it will display information while it is doing things. Next, all the object files that we want are placed. ar is actually a much more generic command than all this and is used to pack any type of file (not just object files). It also has options to see what files are inside, delete some of them, replace them, etc.
1. Create the object files (.o) from our source code (.c)
?
gcc -c source.c -o source.o
2. Create the libraries (.a)
领英推荐
?
ar -rv libname.a source1.o source2.o … sourcen.o
?
Doing this entire process by hand each time can be a bit of a hassle. The usual thing is to make a file called Makefile in the same directory where the library sources are and use make to compile it. If you don't know what I'm talking about, take a look at the makefiles page. Fortunately, make's implicit rules already know how to make static libraries. The Makefile file would be as simple as this:
?
Makefile
CFLAGS=-I<path1> -I<path2> ...
libnombre.a: libnombre.a (objeto1.o ojbeto2.o ...)
?
?
?
How to use libraries in C?
?
Method 1: Use libraries in the same directory #include “library. h”
1. Create the header file. We create a file with the extension “. ...
2. Create the library code file. ...
3. Call the bookstore.