Linux-Dynamic Linking vs. Static Linking (Dynamic Library and Static Library)

First, the basic concept of the library:

A large number of libraries exist on both the windows platform and the linux platform. Essentially, a library is a binary form of executable code that can be loaded into memory for execution by the operating system. Due to the different nature of windows and linux, the binaries of the two libraries are incompatible. In layman's terms, the object files of these commonly used functions are packaged together to provide the interface of the corresponding function, which is convenient for programmers to use. When using the function, only the header file corresponding to the package is needed. According to the use of the library, it can be divided into dynamic library and static library, and the corresponding suffixes are different on different platforms.

Under WINDOWS: .dll suffix is ​​dynamic library, .lib suffix is ​​static library;

Under LINUX: .so suffix is ​​dynamic library, .a suffix is ​​static library

Two, static library and static link

      <1> Static library:

       The static library can be simply regarded as a set of target files, that is, files formed by compressing and packaging many target files. For example, in our daily programming, if you need to use the printf function, you need the library file package stdio.h. When using strlen, you need the library file package string.h, but if you compile the corresponding function source code directly. The file is provided directly to us, which will cause great inconvenience to our management and use, so we can use the "ar" compression program to compress these target files together to form the libx.a static library file. 

       Note: The naming format of the static library: lib + "library name" + .a (suffix) Example: libadd.a is a static library called add

     <2> Static link:

      For static libraries, when the program is compiled and linked, the library code is linked into the executable file, and the static library is no longer needed when the program runs. In the process of using, we only need to link the library and the compiled files of our program to form an executable file.

     Note: For details about static libraries and static links, please refer to Chapter 4 of "Self-cultivation of programmers-linking, loading and libraries"

Through an example to understand how to compile and link our own header file and code at the same time, and finally generate an executable file:

/////main.c/////

#include <stdio.h>
#include "add.h"

int main()
{
    int ret = add(3, 4);
    printf("3 + 4 = %d\n",ret);

    return 0;
}

/////add.c/////

#include "add.h"

int add( int x, int y)
{   
    return x + y;
}


/////add.h/////

#pragma once
#include <stdio.h>

int add( int x, int y);

/////Makefile/////

main : main.c libadd.a
    gcc main.c -L . -ladd -o main
    //-L为指定路径 .为当前目录下 -l+库名字,编译器可在指定目录下自己寻找名为add的库文件

libadd.a : 
    gcc -c add.c -o add.o

    //ar -rc将多个编译后的文件打包为一个静态库文件
    ar -rc libadd.a add.o

.PHONY:clean
clean:
    rm main libadd.a

Output screenshot after make:

Write a picture description here

<3> Disadvantages:

1. Waste of memory and disk space: The static linking method is very serious for the waste of computer memory and disk space. If the size of a static library in the C language is 1MB, there are 100 files in the system that need to use the library file. If static linking is used, 100M of memory will be wasted. If the number is larger, the waste will be more. For example, the following figure: Both program 1 and program 2 need to use Lib.o. If static linking is used, then two copies of this file will be stored in physical memory .

Write a picture description here

2. Update trouble: For example, 20 modules of a program, each module is only 1MB, then every time you update any one module, the user must re-download the 20M program.

Three, dynamic library and dynamic link

     <1> Dynamic library: the program only links the code of the dynamic library when the program is running, and multiple programs share the code of the library. An executable file linked with a dynamic library contains only a list of the function entry addresses it uses, rather than the entire machine code of the object file where the external function is located.

     Note: The naming format of the dynamic library: lib + "library name" + .so (suffix) Example: libadd.so is a dynamic library called add
    <2> dynamic linking: due to static linking waste memory and module update difficulties Proposed dynamic link. The basic implementation idea is to split the program into relatively independent parts according to modules, and link them together to form a complete program when the program is running, instead of linking all program modules into a single executable file. So dynamic linking is to delay the linking process to run time.

    Similarly, if there are three files: program 1, program 2, and Lib.o, program 1 and program 2 need to use the Lib.o file when executing. When running program 1, the system first loads program 1, when it is found that it needs When the Lib.o file is also loaded into the memory, then go to load program 2 When you find that you also need to use the Lib.o file, you do not need to reload Lib.o, just link the program 2 and the Lib.o file Just get up, there is always only one Lib.o file in the memory.
Write a picture description here

> Note: For details about dynamic libraries and dynamic links, please refer to Chapter 7 of "Programmer's Self-cultivation-Linking, Loading and Libraries".

The dynamic library and dynamic link examples still use the above code, and the output is the same. The only thing that needs to be changed is the Makefile.

/////Makefile/////
main : main.c libadd.so
    gcc main.c -L . -ladd -o main

libadd.so : 
    gcc -fPIC -shared add.c -o libadd.so
    //-shared表示输出结果是共享库类型的  -fPIC表示使用地址无关代码奇数来生产输出文件

.PHONY:clean
clean:
    rm main libadd.so

Note: <1> After we generate the executable file, we can use the ldd command to view the dynamic library that the executable file depends on.

Write a picture description here

<2> As mentioned earlier, the suffixes of library files under Windows and Linux are different. The more fundamental reason is that the file formats of the two files are different. You can check the file type of the dynamic library under Linux through file a dynamic library is actually in ELF format. ELF dynamic link files are called Dynamic Shared Objects (DSO, Dynamic Shared Objects), referred to as shared objects; under windows, dynamic link files are called Dynamic Linking Library (Dynamic Linking Library), which is the full name of the .dll file suffix
< 3> Advantages: ①The undoubtedly saves memory; ②Reduces physical page swap-in and swap-out; ③When upgrading a module, in theory, only need to overwrite the corresponding old target file. The new version of the target file will be automatically loaded into memory and linked; ④ When the program is running, you can dynamically choose to load various program modules to achieve program expansion.

Published 25 original articles · praised 8 · 20,000+ views

Guess you like

Origin blog.csdn.net/boazheng/article/details/104358850