In depth discussion of the Linux static library and dynamic library in detail

  • 2020-04-02 00:45:08
  • OfStack

A library is essentially a binary format of executable code that can be loaded into memory for execution. There are two kinds of libraries: static library and dynamic library.
One, the difference between static library and dynamic library
1. Static library
The name of such libraries is usually libxxx.a; Using the static function library to compile the file is relatively large - space, because all the data of the entire function library will be integrated into the object code, its advantage is obvious, namely, the compiled program does not need external function library support, because all the functions used have been compiled. This can be a disadvantage, of course, because if the static library changes, your program must be recompiled.
2. Dynamic function library
The name of such libraries is usually libxxx.so; Compared with the static function library, the dynamic function library is not compiled into the object code at the time of compilation, your program only calls the corresponding function in the function library when it executes the relevant function, so the dynamic function library produces a smaller executable file. Because function libraries are not integrated into your program, but are dynamically requested and called by the program runtime - time, the program's running environment must provide the appropriate libraries. Dynamic library changes do not affect your program, so dynamic library upgrades/updates more convenient.
 
Two, static library

(1) brief introduction
/ opt/hisi - Linux/x86 - arm/GCC - rule 3.4.3 - uClibc - 0.9.28 / usr/bin/arm - hismall - Linux - GCC \
  Main.c SRC /* -i./ include-l./ lib-mpi-o main
      /opt/hisi-linux/x86-arm/ GCC - 3.4.3-uclibc -0.9.28/usr/bin/arm- hismm-linux-gcc is a cross-compilation tool chain
      \ is a newline, indicating that the next line is the same as the action when, and there should be no space after '\'
      Main.c is the main function
      SRC /* is the source file
      -I back connector file
        -l followed by the library file path
        -l is followed by the library file name, whose full name is libmpi.a
        A is the static library

(ii) writing and using static libraries
(1) design library source pr1.c, pr2.c and main.c

[bill@billstone make_lib]$ cat pr1.c 
#include <stdio.h>
void print1(void) 
{ 
                printf("This is the first lib src!n"); 
} 
[bill@billstone make_lib]$ cat pr2.c 
#include<stdio.h>
void print2(void) 
{ 
                printf("This is the second src lib!n"); 
} 
[bill@billstone make_lib]$ cat main.c 
int main(void) 
{ 
                print1(); 
                print2(); 
                return 0; 
}  

(2)   Compile pr1.c, pr2.c files

[bill@billstone make_lib]$ gcc -O -c pr1.c pr2.c 
[bill@billstone make_lib]$ ls -l pr*.o 
-rw-rw-r--        1 bill          bill                    804    4  month   15 11:11 pr1.o 
-rw-rw-r--        1 bill          bill                    804    4  month   15 11:11 pr2.o 

(3)   Linked static library
In order to find the library file correctly in the compiler, the static library must be named according to the rule lib[name]. A, in the following example [name]=pr.
Significance of ar parameter:
R: insert the module (replace) into the library. When the inserted module name already exists in the library, the module with the same name is replaced.
S: writes an object file index to the library, or updates an existing object file index.
V: this option is used to display additional information to perform the action option.
T: displays the list of module tables for the library. Typically, only the module name is displayed.
[bill@billstone make_lib]$ar-rsv libpr.a pr1.o pr2.o
A - pr1. O
A - pr2. O
[bill@billstone make_lib]$ar-t libpr.a
Pr1. O
Pr2. O
(4)   Compile link options
-l and -l parameters are placed after. Where,-l loads the library file path,-l indicates the library file name.
[bill@billstone make_lib]$gcc-o main main.c-l./ -lpr         / / generated main
(5) execute the target program
[bill @ billstone make_lib] $. / main
This is the first lib SRC!
This is the second SRC lib!

Iii. Dynamic library (implicitly called)
(1) design library code  

[bill@billstone make_lib]$ cat pr1.c 
#include <stdio.h>
int p = 2; 
void print(){ 
                printf("%p:%dn", &p, p);
                printf("This is the first dll src!n"); 
} 

(2) generate dynamic library   XXX. So

[bill@billstone make_lib]$ gcc -O -fpic -shared -o xxx.so pr1.c 
[bill@billstone make_lib]$ ls -l *.so 
-rwxrwxr-x        1 bill          bill                  6592    4  month   15 15:19 xxx.so 

(3) implicit invocation of dynamic library  

[bill@billstone make_lib]$ cat main.c 
int main() 
{ 
       print(); 
       return 0; 
}
[bill@billstone make_lib]$ gcc -o main main.c ./xxx.so 
[bill@billstone make_lib]$ ./main 
0x97b5d4:2
this is the first lib src!

When the location of the dynamic library changes,   The program will not work properly;   One of the benefits of replacing a static library with a dynamic library is that you can update the contents of the library at any time by updating the dynamic library.

Related articles: