search
HomeOperation and MaintenanceLinux Operation and MaintenanceWhat 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.

What is the difference between static link library and dynamic link library under Linux?

#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 add

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

Output screenshot after make:

What is the difference between static link library and dynamic link library 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 is the difference between static link library and dynamic link library under Linux?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文件。
What is the difference between static link library and dynamic link library 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 is the difference between static link library and dynamic link library under Linux?

  • 前面提到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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
什么是linux设备节点什么是linux设备节点Apr 18, 2022 pm 08:10 PM

linux设备节点是应用程序和设备驱动程序沟通的一个桥梁;设备节点被创建在“/dev”,是连接内核与用户层的枢纽,相当于硬盘的inode一样的东西,记录了硬件设备的位置和信息。设备节点使用户可以与内核进行硬件的沟通,读写设备以及其他的操作。

Linux中open和fopen的区别有哪些Linux中open和fopen的区别有哪些Apr 29, 2022 pm 06:57 PM

区别:1、open是UNIX系统调用函数,而fopen是ANSIC标准中的C语言库函数;2、open的移植性没fopen好;3、fopen只能操纵普通正规文件,而open可以操作普通文件、网络套接字等;4、open无缓冲,fopen有缓冲。

linux中什么叫端口映射linux中什么叫端口映射May 09, 2022 pm 01:49 PM

端口映射又称端口转发,是指将外部主机的IP地址的端口映射到Intranet中的一台计算机,当用户访问外网IP的这个端口时,服务器自动将请求映射到对应局域网内部的机器上;可以通过使用动态或固定的公共网络IP路由ADSL宽带路由器来实现。

linux中eof是什么linux中eof是什么May 07, 2022 pm 04:26 PM

在linux中,eof是自定义终止符,是“END Of File”的缩写;因为是自定义的终止符,所以eof就不是固定的,可以随意的设置别名,linux中按“ctrl+d”就代表eof,eof一般会配合cat命令用于多行文本输出,指文件末尾。

什么是linux交叉编译什么是linux交叉编译Apr 29, 2022 pm 06:47 PM

在linux中,交叉编译是指在一个平台上生成另一个平台上的可执行代码,即编译源代码的平台和执行源代码编译后程序的平台是两个不同的平台。使用交叉编译的原因:1、目标系统没有能力在其上进行本地编译;2、有能力进行源代码编译的平台与目标平台不同。

linux怎么判断pcre是否安装linux怎么判断pcre是否安装May 09, 2022 pm 04:14 PM

在linux中,可以利用“rpm -qa pcre”命令判断pcre是否安装;rpm命令专门用于管理各项套件,使用该命令后,若结果中出现pcre的版本信息,则表示pcre已经安装,若没有出现版本信息,则表示没有安装pcre。

linux怎么查询mac地址linux怎么查询mac地址Apr 24, 2022 pm 08:01 PM

linux查询mac地址的方法:1、打开系统,在桌面中点击鼠标右键,选择“打开终端”;2、在终端中,执行“ifconfig”命令,查看输出结果,在输出信息第四行中紧跟“ether”单词后的字符串就是mac地址。

linux中rpc是什么意思linux中rpc是什么意思May 07, 2022 pm 04:48 PM

在linux中,rpc是远程过程调用的意思,是Reomote Procedure Call的缩写,特指一种隐藏了过程调用时实际通信细节的IPC方法;linux中通过RPC可以充分利用非共享内存的多处理器环境,提高系统资源的利用率。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft