C语言 静态库和动态库有什么区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20229364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between a static library and a dynamic one
提问by KaramJaber
In C language what is the difference between a static libraray and a dynamic library?
在 C 语言中,静态库和动态库有什么区别?
回答by Paul92
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.
正如我在回答开头提到的,这是一个非常简化的总结,让您有一个基本的了解。我强烈建议你更多地研究这个主题。
回答by fernando.reyes
In 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 文件而改变。

