Home  >  Article  >  Operation and Maintenance  >  What are the differences between static link libraries and dynamic link libraries under Linux?

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

王林
王林forward
2023-05-17 18:39:332441browse

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:yisu.com. If there is any infringement, please contact admin@php.cn delete