静态库和动态库之间有什么区别[重复]

时间:2022-07-21 13:24:33

This question already has an answer here:

这个问题在这里已有答案:

In C language what is the difference between a static libraray and a dynamic library?

在C语言中,静态库和动态库有什么区别?

2 个解决方案

#1


38  

This concept might be a little bit too broad to explain, but i will try to give you a basic idea from which you can study further.

这个概念可能有点过于宽泛而无法解释,但我会尝试给你一个基本的想法,你可以从中进一步研究。

Firstly, you need to know what a library is. Basically, a library is a collection of functions. You may have noticed that we are using functions which are not defined in our code, or in that particular file. To have access to them, we include a header file, that contains declarations of those functions. After compile, there is a process called linking, that links those function declarations with their definitions, which are in another file. The result of this is the actual executable file.

首先,您需要知道库是什么。基本上,库是函数的集合。您可能已经注意到我们正在使用未在我们的代码中或在该特定文件中定义的函数。要访问它们,我们包含一个头文件,其中包含这些函数的声明。编译之后,有一个名为链接的进程,它将这些函数声明与它们的定义联系起来,这些声明位于另一个文件中。结果是实际的可执行文件。

Now, the linking as I described it is a static linking. This means that every executable file contains in it every library (collection of functions) that it needs. This is a waste of space, as there are many programs that may need the same functions. In this case, in memory there would be more copies of the same function. Dynamic linking prevents this, by linking at the run-time, not at the compile time. This means that all the functions are in a special memory space and every program can access them, without having multiple copies of them. This reduces the amount of memory required.

现在,我所描述的链接是静态链接。这意味着每个可执行文件都包含它所需的每个库(函数集合)。这是浪费空间,因为有许多程序可能需要相同的功能。在这种情况下,在内存中会有更多相同功能的副本。动态链接通过在运行时链接而不是在编译时进行链接来防止这种情况。这意味着所有函数都在一个特殊的内存空间中,每个程序都可以访问它们,而不需要多个副本。这减少了所需的内存量。

As I mentioned at the beginning of my answer, this is a very simplified summary to give you a basic understanding. I strongly suggest you study more on this topic.

正如我在回答的开头所提到的,这是一个非常简化的摘要,可以让您基本了解。我强烈建议你更多地研究这个话题。

#2


2  

In windows:

在Windows中:

The static library is a .lib file that will be linked inside your executable and won't change with time.

静态库是一个.lib文件,它将在您的可执行文件中链接,并且不会随时间变化。

The dynamic library is a .dll file linked to your executable and may change depending on the dll file you load when you execute it.

动态库是链接到您的可执行文件的.dll文件,可能会根据您执行时加载的DLL文件而更改。

#1


38  

This concept might be a little bit too broad to explain, but i will try to give you a basic idea from which you can study further.

这个概念可能有点过于宽泛而无法解释,但我会尝试给你一个基本的想法,你可以从中进一步研究。

Firstly, you need to know what a library is. Basically, a library is a collection of functions. You may have noticed that we are using functions which are not defined in our code, or in that particular file. To have access to them, we include a header file, that contains declarations of those functions. After compile, there is a process called linking, that links those function declarations with their definitions, which are in another file. The result of this is the actual executable file.

首先,您需要知道库是什么。基本上,库是函数的集合。您可能已经注意到我们正在使用未在我们的代码中或在该特定文件中定义的函数。要访问它们,我们包含一个头文件,其中包含这些函数的声明。编译之后,有一个名为链接的进程,它将这些函数声明与它们的定义联系起来,这些声明位于另一个文件中。结果是实际的可执行文件。

Now, the linking as I described it is a static linking. This means that every executable file contains in it every library (collection of functions) that it needs. This is a waste of space, as there are many programs that may need the same functions. In this case, in memory there would be more copies of the same function. Dynamic linking prevents this, by linking at the run-time, not at the compile time. This means that all the functions are in a special memory space and every program can access them, without having multiple copies of them. This reduces the amount of memory required.

现在,我所描述的链接是静态链接。这意味着每个可执行文件都包含它所需的每个库(函数集合)。这是浪费空间,因为有许多程序可能需要相同的功能。在这种情况下,在内存中会有更多相同功能的副本。动态链接通过在运行时链接而不是在编译时进行链接来防止这种情况。这意味着所有函数都在一个特殊的内存空间中,每个程序都可以访问它们,而不需要多个副本。这减少了所需的内存量。

As I mentioned at the beginning of my answer, this is a very simplified summary to give you a basic understanding. I strongly suggest you study more on this topic.

正如我在回答的开头所提到的,这是一个非常简化的摘要,可以让您基本了解。我强烈建议你更多地研究这个话题。

#2


2  

In windows:

在Windows中:

The static library is a .lib file that will be linked inside your executable and won't change with time.

静态库是一个.lib文件,它将在您的可执行文件中链接,并且不会随时间变化。

The dynamic library is a .dll file linked to your executable and may change depending on the dll file you load when you execute it.

动态库是链接到您的可执行文件的.dll文件,可能会根据您执行时加载的DLL文件而更改。