C static libraries
in computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable. This executable and the process of compiling it are both known as a static build of the program. Historically, libraries could only be static. Static libraries are either merged with other static libraries and object files during building/linking to form a single executable or loaded at run-time into the address space of their corresponding executable at a static memory offset determined at compile-time/link-time.?
Why use libraries in C?
Libraries in C are not unlike public libraries in cities, towns, or neighborhoods. A public library provides access to a multitude of information in various media forms to the public for access and use. Functions in a C library can be used and accessed by programmers to create several different programs.
As a programmer, you may find yourself using the same function or functions repeatedly. In this case, it is best to put this function or functions in a library to speed up the compilation of the program. C libraries store files in object code; during the linking phase of the compilation process ( Compilation Process) files in object code are accessed and used. It is faster to link a function from a C library than to link object files from a separate memory sticks or discs.
How they work ?
Here is briefly how C libraries actually work ...
In addition static libraries are added during the linker phase of the compilation process (above). During the linker phase: the linker links access all the libraries to link functions to the program. Static libraries are merged with other static libraries to create an executable program. During the compilation of a program, the static library is called to execute the program.
How to create them ?
Static libraries are created using some type of archiving software, such as ar. ar takes one or more object files (that end in .o), zips them up, and generates an archive file (ends in .a) — This is our “static library”.
Before using ar, we must first have some object files to give to it. Perhaps we’ve written some functions in C that we want to include in our library. We can use the -c option with the GNU compiler (gcc) to stop the compiling process after the assembling stage, translating our files from .c to .o.
$ gcc -c *.c // produces a .o object file
Now that we have the object file(s), we can archive them and make a static library using ar.
$ ar -rc libmy.a *.o
The command above will create a static library called “libmy.a”. Inside of it will be our sum() function that has been translated to an object file via the gcc command earlier.
Note: the -rc options create the archive without a warning and replaces any pre-existing object files in the library with the same name.
Some archivers automatically organize and index the library, but in case indexing didn’t occur, we can use a command called ranlib to generate and store an index in the archive.
领英推荐
If needed use the ‘ranlib <libmy.a> to index the library.
$ ranlib libholberton.a
The index lists each symbol defined by a member of an archive that is a relocatable object file. To see a list of the symbols from object files we can use a command called nm.
$nm libforme.a
// sample outputsum.o:000000000000002e T sum
So now that we’ve created object files, zipped them in up an library and indexed it, we are ready to use our library.
To view the contents of the static library access it using: ‘ar -t libmy.a’ in the command line.
How to use them ?
Let’s say we want to compile our C program from the beginning of this post; the one that uses sum() to store the value of two numbers:
int main(void
{
int x = 5;
int y = 8;
int result; result = sum(x, y);
return (0);
})
When we try to compile the program we might see an error like this:
gcc my_program.c// oops... ///tmp/ccGLAk66.o: In function
`main':my_program.c:(.text+0x26): undefined reference to `sum'collect2:
error: ld returned 1 exit status
The compiler doesn’t know what sum is. We need to tell it to look in our library:
gcc my_program.c -L. -llibmy -o my_program
Let’s break that down:
If everything worked out, the result will be an executable file called my_program that uses the sum() function that is contained in the libmy.a static library.
That's all folks hopefully, you found this useful. Until the next blog smell you later .
Data Scientist
2 年Good job Dali :D Why not start a Medium blog?