DLL和LIB文件 - 什么和为什么? [英] DLL and LIB files - what and why?

查看:152
本文介绍了DLL和LIB文件 - 什么和为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常了解DLL和LIB,除了它们包含程序正常运行所需的重要代码 - 库。但是编译器为什么会产生它们呢?将所有代码包含在单个可执行文件中是不是更容易? DLL和LIB之间有什么区别?

解决方案

有静态库(LIB)和动态库(DLL)。



使用库是因为您可能有许多程序中要使用的代码。例如,如果您编写一个计算字符串中的字符数的函数,该函数将在许多程序中很有用。一旦使该函数正常工作,您就不需要在每次使用该函数时重新编译代码,因此将该函数的可执行代码放在一个库中,链接器可以将编译的代码提取并插入到程序中。为此,静态库有时被称为归档。



动态库更进一步。将图书馆功能的多个副本占用每个程序的空间似乎是浪费的。为什么他们不能共享功能的一个副本?这就是动态库。而是在编译时将库代码构建到程序中,当它被加载到内存中运行时,可以将其映射到程序中。同时运行多个使用相同功能的程序可以共享一个副本,从而节省内存。实际上,您可以根据需要加载动态库,具体取决于代码的路径。如果您没有进行任何打印,则无需打印机程序占用内存。另一方面,这意味着您必须在程序运行的每台机器上安装动态库的副本。这就产生了自己的一套问题。



举个例子,几乎所有用'C'编写的程序都需要一个名为'C运行时库的库'程序将需要所有的功能。 C运行时具有静态和动态版本,因此您可以根据特定需要确定程序使用哪个版本。


I know very little about DLL's and LIB's other than that they contain vital code required for a program to run properly - libraries. But why do compilers generate them at all? Wouldn't it be easier to just include all the code in a single executable? And what's the difference between DLL's and LIB's?

解决方案

There are static libraries (LIB) and dynamic libraries (DLL).

Libraries are used because you may have code that you want to use in many programs. For example if you write a function that counts the number of characters in a string, that function will be useful in lots of programs. Once you get that function working correctly you don't want to have to recompile the code every time you use it, so you put the executable code for that function in a library, and the linker can extract and insert the compiled code into your program. Static libraries are sometimes called 'archives' for this reason.

Dynamic libraries take this one step further. It seems wasteful to have multiple copies of the library functions taking up space in each of the programs. Why can't they all share one copy of the function? This is what dynamic libraries are for. Rather then build the library code into your program when it is compiled, it can be mapped into your program as it is loaded into memory to run. Multiple programs running at the same time that use the same functions can all share one copy, saving memory. In fact, you can load dynamic libraries only as needed, depending on the path through your code. No point in having the printer routines taking up memory if you aren't doing any printing. On the other hand, this means you have to have a copy of the dynamic library installed on every machine your program runs on. This creates its own set of problems.

As an example, almost every program written in 'C' will need functions from a library called the 'C runtime library, though few programs will need all of the functions. The C runtime comes in both static and dynamic versions, so you can determine which version your program uses depending on particular needs.

这篇关于DLL和LIB文件 - 什么和为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆