lib和.dll是什么,他们之间的区别是什么

问题

我知道dll和lib包含了程序运行时必不可少的代码,但是除此之外我对lib和dll 了解不多。编译器究竟为什么生成这两个文件,直接把代码包含在exe里面不是更简单吗?lib和dll又有什么区别?

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?

回答

\qquad 首先一共有两种库,静态库lib和动态库dll。其中 lib可以是静态库(包含了目标文件),也可以是输入库(包含了帮助linker连接到dll的symbols)

There are static libraries (LIB) and dynamic libraries (DLL) - but note that .LIB files can be either static libraries (containing object files) or import libraries (containing symbols to allow the linker to link to a DLL).

\qquad 库之所以存在,是因为人们想在很多程序中重复使用一段代码。比如你写了一个计算字符串中字符数目的函数,这个函数将会在很多程序中使用。一旦该函数正确工作,您就不希望每次使用它时都必须重新编译代码,因此您可以将该函数的可执行代码放入库中,链接器可以提取并将编译后的代码插入程序中。出于这个原因,静态库有时被称为“存档”。

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.

\qquad 动态库在这方面更进一步。让库函数的多个副本在每个程序中占用空间似乎很浪费。为什么他们不能共享一个函数的副本?这就是动态库的作用所在。与其在编译程序时将库代码构建到程序中,不如在将库代码加载到内存时将其映射到程序中来运行。同时运行的多个使用相同函数的程序可以共享一个副本,节省内存。事实上,您可以只在需要时加载动态库,这取决于代码中的路径。如果你不打印任何东西,让打印机例程占用内存是没有意义的。另一方面,这意味着必须在运行程序的每台机器上安装一个动态库的副本。这就产生了一系列问题。

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 than building the library code into your program when it is compiled, it can be run by mapping it into your program as it is loaded into memory. 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.

\qquad 例如,几乎所有用’C’编写的程序都需要’C运行时库’中的函数,尽管很少有程序需要所有的函数。C运行时有静态版本和动态版本,因此您可以根据特定的需要确定程序使用哪个版本。

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.

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值