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

青灯夜游
Release: 2023-02-06 15:09:36
Original
2099 people have browsed it

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
Copy after login

Output screenshot after make:

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

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

<1>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

<2>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
Copy after login

  • 当我们生成可执行文件后,可使用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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!