Linux programming (a) - static library

static library overview | | | (degree mother wave) (度娘一波)

static library, also known as archive file (archive), refers to in our application, there are Some common code is used repeatedly to compile the code into a "library" file; in the linking step, the linker takes the required code from the library file and copies it into the library in the generated executable.

Program compilation generally requires several steps of preprocessing, compiling, assembling, and linking. The static library feature is a complete copy of the library code contained in the executable file; the disadvantage is that there are multiple redundant copies when used multiple times.

Static library (*.a) (where .a refers to) and dynamic library (*.) are two ways to share program code. The difference is that the static library is copied during the link phase of the program. In the program, it has nothing to do with the program running; the dynamic library is not copied into the program during the link phase, but the program is dynamically loaded into the memory for the program call at runtime. The advantage of using a dynamic library is that the system only needs to load the dynamic library once. Different programs can get a copy of the same dynamic library in memory, thus saving a lot of memory.

Create and use static library

Compile process at a glance

预.i .ii file gcc -E hello.cpp -o hello.i

compile.s file gcc -S hello .cpp -o hello.s

`s.o .obj file gcc -c hello.cpp -o hello.o

link.lib .a file

Suppose we have two files now. Main.cpp and func.cpp, main.cpp uses the functions in func.cpp, then after the assembly finishes generating the target file, the linker will link the two files into an executable (.out) file at link time. Here is the example

//main.c
#include<stdlib.h>
#include"lib.h" //Head file

Int main()
{
    Func("Hello World"); //Declared in the lib.h header file
    Exit(0);
}
//Head file lib.h
Void func(char* );
//func.c
#include<stdio.h>
void func(char* a)
{
    printf("%s\n",a);
}

process:

1. Using gcc -c to generate the target file

gcc -c main.c func.c

will generate the main.o and func.o target files

2. To link to the executable

gcc -o proexec main.o func.o

this command will generate an executable called proexec, you can see the output "hello world" directly.

The above compilation process specifically separates the linking process separately, because when we use the static library, it is actually the process of generating the executable file together with the target file at the time of linking. Suppose we have another file, there are other functions we don't use

//otherfunc.c
#include<stdlio.h>

void otherfunc(int arg)
{
    printf("other func\n");
}

We don't want to use other functions when we have to write a command, I want to package them into a static library file, use any later. In this library to the function, you only need to join the path of this library can be linked into an executable file, without the trouble of gcc -o xo yo zo. We need the following command:

gcc -c other.c / / compiled into the target file 
ar crv lib.a other.o func.o / / packaged into a static library 

after execution, A static library file will be generated.

gcc -o proexec2 main.o lib.a

This time there is no need to assign a function, directly link the static library, it will generate the executable file proexec2.

shared library and static library we need

static library has a shortcoming When you run multiple programs at the same time and they all use functions of the same function library, there will be multiple copies of the function in memory, which wastes a lot of memory resources. Shared libraries can solve this problem.

When a program uses a shared library, its way of linking is this: the program itself does not contain function code, but instead refers to shared code that is accessible at runtime. When the compiled program is loaded into memory for execution, the function reference is parsed and a call to the shared library is made, and the shared library is loaded into memory if necessary.