


What is the difference between static link library and dynamic link library under Linux?
Difference: 1. The suffix of the dynamic library is ".so" and the suffix of the static library is ".a". 2. If the static function library changes, the program must be recompiled; while the change of the dynamic function library does not affect the program. 3. Compared with static libraries, dynamic libraries are not compiled into the target code during compilation. The user's program only calls the corresponding functions in the function library when it executes the relevant functions. Therefore, the executable file generated by the dynamic function library smaller.
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
1. Basic concepts of libraries:
There are a large number of libraries under both windows and linux platforms. Essentially, a library is a binary form of executable code that can be loaded into memory by the operating system for execution. Since the nature of Windows and Linux are different, the binaries of the two libraries are incompatible. In layman's terms, it means to package the target files of these commonly used functions together and provide the interface of the corresponding function for the convenience of programmers. When using a function, you only need to include the corresponding header file. According to how the library is used, it can be divided into dynamic library and static library. The corresponding suffixes are also different on different platforms.
Under WINDOWS: .dll suffix is a dynamic library, .lib suffix is a static library;
Under LINUX: .so
suffix is a dynamic library, .a
suffix is static library.
2. Static library and static linking
##Static library:
A static library can be simply regarded as a collection of target files, that is, files formed by compressing and packaging many target files. For example, in our daily programming, if we need to use the printf function, we need to include the stdio.h library file. When using strlen, we need to include the string.h library file. However, if we directly compile the corresponding function source code to form .o Providing the files directly to us 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 a libx.a static library file. Note: Static library naming format: lib "library name" .a (suffix) Example: libadd.a is a static library called addStatic 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 is running. During use, we only need to link the library and the compiled files of our program together to form an executable file. Let’s take an example to learn how to compile and link the header files and codes we wrote ourselves 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.aOutput screenshot after make:
1. Waste of memory and disk space:
Static linking method is not suitable for computermemory and disk The space wasted is very serious. Suppose the size of a C language static library is 1MB, and there are 100 files in the system that need to use the library. If static linking is used, 100M of memory will be wasted. If the number is larger, it will be wasted. Just more. For example, as shown below: Both Program 1 and Program 2 need to use Lib.o. If static linking is used, two copies of this file will be stored in the physical memory.
2. Updating troubles:
For example, a program has 20 modules, and each module is only 1MB. Then every time you update any module, the user has to Re-download the 20M program.
3. Dynamic library and dynamic link
Dynamic library:The code of the dynamic library is linked when the program is running, and multiple programs share the code of the library. An executable file linked with a dynamic library only contains a table of the entry addresses of the functions it uses, rather than the entire machine code of the target file where the external function is located.
Note: Dynamic library naming format: lib "library name" .so (suffix) Example: libadd.so is a dynamic library called add
Dynamic link :Dynamic linking was proposed because static linking has problems such as wasting memory and difficulty in module update. 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 program module like static linking. executable file. So dynamic linking postpones the linking process until runtime.
同样,假如有程序1,程序2,和Lib.o三个文件,程序1和程序2在执行时都需要用到Lib.o文件,当运行程序1时,系统首先加载程序1,当发现需要Lib.o文件时,也同样加载到内存,再去加载程序2当发现也同样需要用到Lib.o文件时,则不需要重新加载Lib.o,只需要将程序2和Lib.o文件链接起来即可,内存中始终只存在一份Lib.o文件。
动态库和动态链接的例子依然使用上面的代码,输出结果也相同,唯一需要改变的就是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
当我们生成可执行文件后,可使用ldd命令查看该可执行文件所依靠的动态库。
前面提到windows和Linux下库文件的后缀不同,更根本的原因在于二者文件格式都不同。可以通过file一个动态库查看Linux下动态库的文件类型其实是ELF格式。ELF动态链接文件被称为动态共享对象(DSO,Dynamic Shared Objects),简称共享对象;在windows下,动态链接文件被称为动态链接库(Dynamic Linking Library),也就是.dll文件后缀的全称。
优点:
①毋庸置疑的就是节省内存;
②减少物理页面的换入换出;
③在升级某个模块时,理论上只需要将对应旧的目标文件覆盖掉即可。新版本的目标文件会被自动装载到内存中并且链接起来;
④程序在运行时可以动态的选择加载各种程序模块,实现程序的扩展。
四、静态库和动态库的区别
1. 静态库
这类库的名字一般是 libxxx.a ;利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持,因为所有使用的函数都已经被编译进去了。当然这也会成为他的缺点,因为 如果静态函数库改变了,那么你的程序必须重新编译 。
2. 动态库
这类库的名字一般是 libxxx.so ;相对于静态函数库,动态函数库在编译的时候并没有被编译进目标代码中,你的程序执行到相关函数时才调用该函数库里的相应函数,因此动态函数库所产生的可执行文件比较小。由于函数库没有被整合进你的程序,而是程序运行时动态的申请并调用,所以程序的运行环境中必须提供相应的库。 动态函数库的改变并不影响你的程序,所以动态函数库的升级比较方便。
相关推荐:《Linux视频教程》
The above is the detailed content of What is the difference between static link library and dynamic link library under Linux?. For more information, please follow other related articles on the PHP Chinese website!

Shell scripts are powerful tools for automated execution of commands in Linux systems. 1) The shell script executes commands line by line through the interpreter to process variable substitution and conditional judgment. 2) The basic usage includes backup operations, such as using the tar command to back up the directory. 3) Advanced usage involves the use of functions and case statements to manage services. 4) Debugging skills include using set-x to enable debugging mode and set-e to exit when the command fails. 5) Performance optimization is recommended to avoid subshells, use arrays and optimization loops.

Linux is a Unix-based multi-user, multi-tasking operating system that emphasizes simplicity, modularity and openness. Its core functions include: file system: organized in a tree structure, supports multiple file systems such as ext4, XFS, Btrfs, and use df-T to view file system types. Process management: View the process through the ps command, manage the process using PID, involving priority settings and signal processing. Network configuration: Flexible setting of IP addresses and managing network services, and use sudoipaddradd to configure IP. These features are applied in real-life operations through basic commands and advanced script automation, improving efficiency and reducing errors.

The methods to enter Linux maintenance mode include: 1. Edit the GRUB configuration file, add "single" or "1" parameters and update the GRUB configuration; 2. Edit the startup parameters in the GRUB menu, add "single" or "1". Exit maintenance mode only requires restarting the system. With these steps, you can quickly enter maintenance mode when needed and exit safely, ensuring system stability and security.

The core components of Linux include kernel, shell, file system, process management and memory management. 1) Kernel management system resources, 2) shell provides user interaction interface, 3) file system supports multiple formats, 4) Process management is implemented through system calls such as fork, and 5) memory management uses virtual memory technology.

The core components of the Linux system include the kernel, file system, and user space. 1. The kernel manages hardware resources and provides basic services. 2. The file system is responsible for data storage and organization. 3. Run user programs and services in the user space.

Maintenance mode is a special operating level entered in Linux systems through single-user mode or rescue mode, and is used for system maintenance and repair. 1. Enter maintenance mode and use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can check and repair the file system and use the command "fsck/dev/sda1". 3. Advanced usage includes resetting the root user password, mounting the file system in read and write mode and editing the password file.

Maintenance mode is used for system maintenance and repair, allowing administrators to work in a simplified environment. 1. System Repair: Repair corrupt file system and boot loader. 2. Password reset: reset the root user password. 3. Package management: Install, update or delete software packages. By modifying the GRUB configuration or entering maintenance mode with specific keys, you can safely exit after performing maintenance tasks.

Linux network configuration can be completed through the following steps: 1. Configure the network interface, use the ip command to temporarily set or edit the configuration file persistence settings. 2. Set up a static IP, suitable for devices that require a fixed IP. 3. Manage the firewall and use the iptables or firewalld tools to control network traffic.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
