Dynamic Libraries vs Static Libraries

1. The compilation and linking process of a source program
A source file written by ourselves will definitely use some library functions. This involves the dynamic linking and static linking of the library. First of all, let's take a look. A source file hello.c consists of A series of steps taken by loading to running
write picture description here
2. Basic concepts of static library and dynamic library
What is a static library and what is the suffix of a static library?
The so-called static library means that the program links the code in the library to the executable file during the process of compiling and linking, and the static library is not needed during the process of the program running.
Under Windows, the suffix of the static library is .lib, and under the Linux system, the suffix of the static library is .a.
What is a dynamic library and what is the suffix of a dynamic library?
The so-called dynamic library refers to the code that links and runs when the program is running, and multiple programs share the code that uses the library.
Under Windows, the suffix of the dynamic library is .dll, and under the Linux system, the suffix of the dynamic library is .so
3. Write and use the static library
(1) First write four files: add.h add.c sub.c sub.h
write picture description here
(2) Generate .o file gcc -c add.c sub.c
write picture description here
(3) Package and generate static library
Use the command: ar -rc libmath.a add.o sub.o
//This is called the math library, and the library must start with lib , the static library ends with .a, and the dynamic library ends with .so
(4) Using the library: To use the library, first import the library and the header file. Use the command to
import the library file: cp ../libmath.a .
Import the header file cp ../*.h .
(5) Write a function for testing
(6) gcc -o main main.c -L.
//Look for the library in the current directory. If the math library is not added here, the compiler will show that
gcc -o main main.c -L. -lmath is not found. // Search under the math library
write picture description here
(7) ./main run
4. Write and use the dynamic library
(1) First write add.h add.c sub.c sub.h four files
write picture description here
(2) Use the library: To use the library, First, import the library into cp ../libmath.a .
Import the header file into cp ../*.h .
(3) Generate the .o file gcc -fPIC -c add.c sub.c
write picture description here
(4) Generate the shared library gcc -shared -o libmath.so *.o
write picture description here
(5) Use of dynamic libraries: import library and header files Import
of header files: cp ../*.h .
Import of libraries: cp ../*.so .
( 6) Create a test function to compile
gcc -o main main.c -L. -lmath
ldd Program name: used to record the dynamic library that an application depends on
(7) Import of library
a) Method 1: Copy the .so file to Under the system shared library path, it generally refers to /usr/lib
b) Method 2: Change the environment variable export LD_LIBRARY_PATH=.
Specify your own directory mkdir /home/wyn/so
Put the dynamic library into the directory specified by yourself, mv libmath.so /home/wyn/so
(8) After importing, you need to add a file. There is no requirement for the file name.
First create a path: cd /etc/ld.so. conf.d/ , create a file so.conf in this path, open so.conf and write the directory you specified before.
(9) Use the ldconfig command to make the directory just imported take effect
(10) Exit back to the previous test directory to compile gcc -o main main.c -L/home/wyn/so -lmath
(11) Run./ main
5. Comparison between static library and dynamic library The
system uses dynamic link library by default
(1) Static link means that each program has a part of its own required code, copy this part of the code from the library, and the library will not be needed in the future;
(2) Dynamic linking needs to rely on dynamic libraries. Static linking has good portability, while dynamic linking libraries have poor portability. Static linking leads to larger program size and smaller dynamic link program size.
(3) The static library is loaded into the memory when the program is running, whether it is called or not, it will be in the memory; the dynamic library is loaded into the memory when it needs to be called when the program is running, and will not be loaded when it is not needed. memory.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326439406&siteId=291194637