C++ 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3250467/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:26:43  来源:igfitidea点击:

What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?

c++dlllinkerstatic-libraries

提问by Sulla

What is inside of a .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?

静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?

How come there is no need for a .lib file in dynamically linked dynamic library and also that in static linking, the .lib file is nothing but a .obj file with all the methods. Is that correct?

为什么在动态链接的动态库和静态链接中不需要 .lib 文件,.lib 文件只不过是一个包含所有方法的 .obj 文件。那是对的吗?

回答by Anthony Williams

For a static library, the .lib file contains all the code and data for the library. The linker then identifies the bits it needs and puts them in the final executable.

对于静态库,.lib 文件包含库的所有代码和数据。然后链接器识别它需要的位并将它们放入最终的可执行文件中。

For a dynamic library, the .lib file contains a list of the exported functions and data elements from the library, and information about which DLL they came from. When the linker builds the final executable then if any of the functions or data elements from the library are used then the linker adds a reference to the DLL (causing it to be automatically loaded by Windows), and adds entries to the executable's import table so that a call to the function is redirected into that DLL.

对于动态库,.lib 文件包含从库中导出的函数和数据元素的列表,以及有关它们来自哪个 DLL 的信息。当链接器构建最终的可执行文件时,如果使用了库中的任何函数或数据元素,则链接器会添加对 DLL 的引用(使其由 Windows 自动加载),并将条目添加到可执行文件的导入表中,以便对该函数的调用被重定向到该 DLL 中。

You don't need a .lib file to use a dynamic library, but without one you cannot treat functions from the DLL as normal functions in your code. Instead you must manually call LoadLibraryto load the DLL (and FreeLibrarywhen you're done), and GetProcAddressto obtain the address of the function or data item in the DLL. You must then cast the returned address to an appropriate pointer-to-function in order to use it.

使用动态库不需要 .lib 文件,但如果没有,则无法将 DLL 中的函数视为代码中的普通函数。相反,您必须手动调用LoadLibrary以加载 DLL(以及FreeLibrary完成后),并GetProcAddress获取 DLL 中函数或数据项的地址。然后,您必须将返回的地址转换为适当的函数指针才能使用它。

回答by irsis

I found following answerfrom Hans also useful here.It clears the air that there could two types of lib files.

我发现Hans 的以下回答在这里也很有用。它澄清了可能有两种类型的 lib 文件的说法。

A LIB file is used to build your program, it only exists on your build machine and you don't ship it. There are two kinds. A static link library is a bag of .obj files, collected into a single file. The linker picks any chunks of code from the file when it needs to resolve an external identifier.

But more relevant to DLLs, a LIB file can also be an import library. It is then a simple small file that includes the name of the DLL and a list of all the functions exported by the DLL. You'll need to provide it to the linker when you build a program that uses the DLL so it knows that an external identifier is actually a function exported by the DLL. The linker uses the import library to add entries to the import table for the EXE. Which is then in turn used by Windows at runtime to figure out what DLLs need to be loaded to run the program.

LIB 文件用于构建您的程序,它只存在于您的构建机器上并且您不发送它。有两种。静态链接库是一包 .obj 文件,收集到一个文件中。当链接器需要解析外部标识符时,它会从文件中选取任何代码块。

但与 DLL 更相关的是,LIB 文件也可以是导入库。然后它是一个简单的小文件,其中包括 DLL 的名称和 DLL 导出的所有函数的列表。当您构建使用 DLL 的程序时,您需要将它提供给链接器,以便它知道外部标识符实际上是由 DLL 导出的函数。链接器使用导入库将条目添加到 EXE 的导入表中。然后 Windows 在运行时使用它来确定需要加载哪些 DLL 来运行程序。

回答by Cogwheel

In a static library, the lib file contains the actual object code for the functions provided by the library. In the shared version (what you referred to as statically linked dynamic library), there is just enough code to establish the dynamic linkage at runtime.

在静态库中,lib 文件包含库提供的函数的实际目标代码。在共享版本(您称为静态链接的动态库)中,只有足够的代码在运行时建立动态链接。

I'm not sure about "dynamically linked dynamic libraries" (loaded programmatically). Do you even link with a .lib in that case?

我不确定“动态链接的动态库”(以编程方式加载)。在这种情况下,您甚至与 .lib 链接吗?

Edit:

编辑:

A bit late in coming, but no, you don't link a .lib. Well, you link to the lib with libraryloaderex in it. But for the actual library you're using, you provide your own bindings via C function pointers and loadlibrary fills those in.

有点晚了,但不,你没有链接 .lib。好吧,您链接到包含 libraryloaderex 的库。但是对于您正在使用的实际库,您可以通过 C 函数指针提供自己的绑定,然后 loadlibrary 填充它们。

Here's a summary:

这是一个总结:

Linking  ∥ Static        | DLL                  | LoadLibrary
=========∥===============|======================|===================
API code ∥ In your com-  | In the DLL           | In the DLL
lives    ∥ piled program |                      |
---------∥---------------|----------------------|-------------------
Function ∥ Direct, may   | Indirect via table   | Indirect via your
calls    ∥ be elided     | filled automatically | own function ptrs
---------∥---------------|----------------------|-------------------
Burden   ∥ Compiler      | Compiler/OS          | You/OS

回答by user34660

A lib files is read by the linker and a dll file is used during execution. A lib file is essentially useless during execution and a linker is incapable of readinga dll file (except possibly in a manner irrelevant here).

链接器读取 lib 文件,执行期间使用 dll 文件。lib 文件在执行期间本质上是无用的,并且链接器无法读取dll 文件(除非可能以与此处无关的方式)。

The differences between the use of lib files for static and dynamic linking might be confusing but if you understand a little history then it becomes very clear.

使用 lib 文件进行静态链接和动态链接之间的差异可能令人困惑,但如果您了解一点历史,就会变得非常清楚。

Originally there were only static libraries. For a static library, the .lib file contains obj files. Each obj file is the output of one and only one compiler source code input file. A lib file is just a collection of related obj files, much like putting obj files in a directory. That is essentially what a lib file is, a library of obj files. For a static link, all of the obj files that an executable uses are combined into one file. Compare that to a dynamic link in which the executable is in a file separate from the other code it uses.

最初只有静态库。对于静态库,.lib 文件包含 obj 文件。每个 obj 文件是一个且只有一个编译器源代码输入文件的输出。lib 文件只是相关 obj 文件的集合,就像将 obj 文件放在目录中一样。这本质上就是一个 lib 文件,一个 obj 文件库。对于静态链接,可执行文件使用的所有 obj 文件都合并为一个文件。将其与动态链接进行比较,其中可执行文件位于与其使用的其他代码分开的文件中。

To implement dynamic linking, Microsoft modified the use of lib files such that they refer to a dll file instead of locations in an obj file. Other than that, all the information that is in a library for a static link is the same as for a dynamic link. They are all the same as far as the information in them except that a lib file for a dynamic link specifies the dll file.

为了实现动态链接,Microsoft 修改了 lib 文件的使用,以便它们引用 dll 文件而不是 obj 文件中的位置。除此之外,静态链接库中的所有信息都与动态链接相同。除了动态链接的 lib 文件指定了 dll 文件之外,它们就其中的信息而言都是相同的。

回答by Quonux

In dll's are "things" like in an exe (there can be any kind of data, imports, exports, read/write/executable sections) but the difference is that an exe file exports only the entry point (function) but dll's export one/many functions.

在 dll 中是“东西”,就像在 exe 中一样(可以有任何类型的数据、导入、导出、读/写/可执行部分),但区别在于 exe 文件仅导出入口点(函数),而 dll 导出入口点(函数) /许多功能。