The library is essentially a binary format of executable code that can be loaded into memory for execution. The database is divided into static and dynamic databases.
I. Differences between static and dynamic libraries
1. Static function library
The class library name is generally libxxx. a. The files compiled using the static function library are relatively large-space, because all the data in the function library will be integrated into the target code. Its advantages are obvious, that is, the compiled execution program does not need the support of external function libraries, because all the functions used have been compiled. Of course, this will also become his shortcoming, because if the static function library changes, then your program must be re-compiled.
2. Dynamic function library
The class library name is generally libxxx. so; compared with the static function library, the dynamic function library is not compiled into the target code during compilation, and the corresponding function in the function library is called only when your program executes the relevant function, therefore, the executable files generated by the dynamic function library are relatively small. Because the function library is not integrated into your program, but dynamically applied for and called when the program is running-time, the corresponding library must be provided in the running environment of the program. Changes to the dynamic function library do not affect your program, so it is easier to upgrade/update the dynamic function library.
Ii. Static Library
(1) Brief Introduction
/Opt/hisi-linux/x86-arm/gcc-3.4.3-uClibc-0.9.28/usr/bin/arm-hismall-linux-gcc \
Main. c src/*-I./include-L./lib-lmpi-o main
/Opt/hisi-linux/x86-arm/gcc-3.4.3-uClibc-0.9.28/usr/bin/arm-hismall-linux-gcc for the cross-compilation toolchain
\ Is a line feed, indicating that the next line is the same as the behavior, '\' cannot be followed by Spaces
Main. c as the main function
Src/* is the source file
-I connector File
-L path of the followed Library File
-L followed by the library file name, full name: libmp I.
. A is a static library
(2) Writing and using static databases
(1) Source Code pr1.c, pr2.c, and main. cCopy codeThe Code is as follows: [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 and pr2.c filesCopy codeThe Code is as follows: [bill @ billstone make_lib] $ gcc-O-c pr1.c pr2.c
[Bill @ billstone make_lib] $ ls-l pr *. o
-Rw-r -- 1 bill 804 April 15 11:11 pr1.o
-Rw-r -- 1 bill 804 April 15 11:11 pr2.o
(3) Link Static Databases
To correctly find the library file in the Compilation Program, the static library must be named according to the lib [name]. a rule, as shown in the following example: [name] = pr.
Ar parameter meaning:
R: Insert a module (replace) into the database ). If the inserted Module name already exists in the Database, replace the module with the same name.
S: Write a target file index to the database, or update an existing target file index.
V: This option is used to display additional information about the operation options.
T: displays the list of modules in the database. Generally, only the module name is displayed.
[Bill @ billstone make_lib] $ ar-respiratory libpr. a pr1.o pr2.o
A-pr1.o
A-pr2.o
[Bill @ billstone make_lib] $ ar-t libpr.
Pr1.o
Pr2.o
(4) Compile link options
-L and-l parameters are placed at the end.-L loads the path of the library file, and-l indicates the name of the library file.
[Bill @ billstone make_lib] $ gcc-o main. c-L./-lpr // generate 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 (implicit call)
(1) Design Database code Copy codeThe Code is as follows: [bill @ billstone make_lib] $ cat pr1.c
# Include <stdio. h>
Int p = 2;
Void print (){
Printf ("% p: % d \ n", & p, p );
Printf ("This is the first dll src! \ N ");
}
(2) generate dynamic library xxx. soCopy codeThe Code is as follows: [bill @ billstone make_lib] $ gcc-O-fpic-shared-o xxx. so pr1.c
[Bill @ billstone make_lib] $ ls-l *. so
-Rwxrwxr-x 1 bill 6592 April 15 15:19 xxx. so
(3) implicit call of the dynamic library
Copy codeThe Code is as follows: [bill @ billstone make_lib] $ cat main. c
Int main ()
{
Print ();
Return 0;
}
[Bill @ billstone make_lib] $ gcc-o 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 be able to run normally. One of the advantages of the dynamic library to replace the static library is to update the library content at any time by updating the dynamic library.