search
HomeOperation and MaintenanceLinux Operation and MaintenanceWhat are the differences between static link libraries and dynamic link libraries under Linux?

Difference: 1. The suffix of the dynamic library is ".so" and the suffix of the static library is ".a". If the dynamic function library changes, the program does not need to be recompiled, but if the static function library changes, the program must be recompiled. 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.

1. Basic concepts of libraries:

There are a large number of libraries under both windows and linux platforms. Libraries are essentially binary forms of executable code that can be loaded into memory and executed by the operating system. Due to fundamental differences between Windows and Linux, the library binaries for these two platforms are incompatible. It can be simply understood as packaging the target files of these commonly used functions and providing corresponding function interfaces to facilitate programmers' use. When using a function, you only need to include the corresponding header file. Dynamic libraries and static libraries are used differently, and their 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:

Simply put , a static library is a collection of files packed and compressed by multiple 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 add

Static link :

The code of the static library is linked into the executable file when compiling and linking, and the program no longer relies on the static library when running. Just link the library file and the file generated by program compilation to generate 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.a

Output screenshot after make:

What are the differences between static link libraries and dynamic link libraries under Linux?

##Disadvantages:

1. Waste of memory and disk space:

Static linking method is not suitable for computer

memory 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.

What are the differences between static link libraries and dynamic link libraries under Linux?2. Update trouble:

For example, if a program consists of 20 modules, and the size of each module is 1MB, then when When updating any module, users need to re-download the entire 20MB program.

3. Dynamic library and dynamic link

Dynamic library:

The program will only go to Code for linking dynamic libraries and code for libraries shared by multiple programs. 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 :

Due to considerations such as memory waste and difficulty in module update, dynamic linking is proposed as an alternative to static linking. 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文件。
What are the differences between static link libraries and dynamic link libraries under Linux?
动态库和动态链接的例子依然使用上面的代码,输出结果也相同,唯一需要改变的就是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命令查看该可执行文件所依靠的动态库。

What are the differences between static link libraries and dynamic link libraries under Linux?

  • Windows和Linux下库文件后缀不同的根本原因是两者文件格式不同。在Linux系统中,我们可以通过使用file命令来检测动态库的文件类型,而其实这些动态库都是以ELF格式存储的。ELF动态链接文件被称为动态共享对象(DSO,Dynamic Shared Objects),简称共享对象;在windows下,动态链接文件被称为动态链接库(Dynamic Linking Library),也就是.dll文件后缀的全称。

优点

  • ①毋庸置疑的就是节省内存;

  • ②减少物理页面的换入换出;

  • 升级某个模块时,通常只需覆盖对应的旧目标文件。新版本的目标文件会被自动装载到内存中并且链接起来;

  • ④程序在运行时可以动态的选择加载各种程序模块,实现程序的扩展。

四、静态库和动态库的区别

1. 静态库

这类库的名字一般是 libxxx.a ;利用静态函数库编译成的文件比较大,因为整个 函数库的所有数据都会被整合进目标代码中,他的优点就显而易见了,即编译后的执行程序不需要外部的函数库支持,因为所有使用的函数都已经被编译进去了。如果静态函数库发生更改,那么你的程序需要重新编译,所以这也会成为他的不足之处。

2. 动态库

这类库的名字一般是 libxxx.so ;相对于静态函数库,动态函数库在编译的时候并没有被编译进目标代码中,你的程序执行到相关函数时才调用该函数库里的相应函数,因此动态函数库所产生的可执行文件比较小。为使程序能够正常运行,需要在程序的运行环境中提供相应的函数库,因为该库无法被整合进程序,而是在程序运行时动态地申请和调用。动态函数库的更新方便,因为它的修改不会影响你的程序。

The above is the detailed content of What are the differences between static link libraries and dynamic link libraries under Linux?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Linux Operations: Networking and Network ConfigurationLinux Operations: Networking and Network ConfigurationApr 27, 2025 am 12:09 AM

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.

Maintenance Mode in Linux: A System Administrator's GuideMaintenance Mode in Linux: A System Administrator's GuideApr 26, 2025 am 12:20 AM

Maintenance mode plays a key role in Linux system management, helping to repair, upgrade and configuration changes. 1. Enter maintenance mode. You can select it through the GRUB menu or use the command "sudosystemctlisolaterscue.target". 2. In maintenance mode, you can perform file system repair and system update operations. 3. Advanced usage includes tasks such as resetting the root password. 4. Common errors such as not being able to enter maintenance mode or mount the file system, can be fixed by checking the GRUB configuration and using the fsck command.

Maintenance Mode in Linux: When and Why to Use ItMaintenance Mode in Linux: When and Why to Use ItApr 25, 2025 am 12:15 AM

The timing and reasons for using Linux maintenance mode: 1) When the system starts up, 2) When performing major system updates or upgrades, 3) When performing file system maintenance. Maintenance mode provides a safe and controlled environment, ensuring operational safety and efficiency, reducing impact on users, and enhancing system security.

Linux: Essential Commands and OperationsLinux: Essential Commands and OperationsApr 24, 2025 am 12:20 AM

Indispensable commands in Linux include: 1.ls: list directory contents; 2.cd: change working directory; 3.mkdir: create a new directory; 4.rm: delete file or directory; 5.cp: copy file or directory; 6.mv: move or rename file or directory. These commands help users manage files and systems efficiently by interacting with the kernel.

Linux Operations: Managing Files, Directories, and PermissionsLinux Operations: Managing Files, Directories, and PermissionsApr 23, 2025 am 12:19 AM

In Linux, file and directory management uses ls, cd, mkdir, rm, cp, mv commands, and permission management uses chmod, chown, and chgrp commands. 1. File and directory management commands such as ls-l list detailed information, mkdir-p recursively create directories. 2. Permission management commands such as chmod755file set file permissions, chownuserfile changes file owner, and chgrpgroupfile changes file group. These commands are based on file system structure and user and group systems, and operate and control through system calls and metadata.

What is Maintenance Mode in Linux? ExplainedWhat is Maintenance Mode in Linux? ExplainedApr 22, 2025 am 12:06 AM

MaintenanceModeinLinuxisaspecialbootenvironmentforcriticalsystemmaintenancetasks.Itallowsadministratorstoperformtaskslikeresettingpasswords,repairingfilesystems,andrecoveringfrombootfailuresinaminimalenvironment.ToenterMaintenanceMode,interrupttheboo

Linux: A Deep Dive into Its Fundamental PartsLinux: A Deep Dive into Its Fundamental PartsApr 21, 2025 am 12:03 AM

The core components of Linux include kernel, file system, shell, user and kernel space, device drivers, and performance optimization and best practices. 1) The kernel is the core of the system, managing hardware, memory and processes. 2) The file system organizes data and supports multiple types such as ext4, Btrfs and XFS. 3) Shell is the command center for users to interact with the system and supports scripting. 4) Separate user space from kernel space to ensure system stability. 5) The device driver connects the hardware to the operating system. 6) Performance optimization includes tuning system configuration and following best practices.

Linux Architecture: Unveiling the 5 Basic ComponentsLinux Architecture: Unveiling the 5 Basic ComponentsApr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

DVWA

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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools