简体   繁体   English

什么是静态库的内部.lib文件,静态链接的动态库和动态链接的动态库?

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

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. 为什么在动态链接的动态库中不需要.lib文件,而且在静态链接中,.lib文件只是一个包含所有方法的.obj文件。 Is that correct? 那是对的吗?

For a static library, the .lib file contains all the code and data for the library. 对于静态库,.lib文件包含库的所有代码和数据。 The linker then identifies the bits it needs and puts them in the final executable. 然后,链接器识别它需要的位并将它们放入最终的可执行文件中。

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. 对于动态库,.lib文件包含库中导出的函数和数据元素的列表,以及有关它们来自哪个DLL的信息。 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. 当链接器构建最终的可执行文件时,如果使用库中的任何函数或数据元素,则链接器会添加对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. 您不需要.lib文件来使用动态库,但如果没有它,则无法将DLL中的函数视为代码中的常规函数​​。 Instead you must manually call LoadLibrary to load the DLL (and FreeLibrary when you're done), and GetProcAddress to obtain the address of the function or data item in the DLL. 相反,您必须手动调用LoadLibrary以加载DLL(以及完成后的FreeLibrary ),并使用GetProcAddress来获取DLL中函数或数据项的地址。 You must then cast the returned address to an appropriate pointer-to-function in order to use it. 然后,必须将返回的地址强制转换为适当的指向函数的指针才能使用它。

I found following answer from 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. LIB文件用于构建程序,它只存在于您的构建计算机上,而您不会发送它。 There are two kinds. 有两种。 A static link library is a bag of .obj files, collected into a single file. 静态链接库是一包.obj文件,收集到一个文件中。 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. 但是与DLL更相关,LIB文件也可以是导入库。 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. 它是一个简单的小文件,包含DLL的名称和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. 在构建使用DLL的程序时,您需要将它提供给链接器,以便它知道外部标识符实际上是DLL导出的函数。 The linker uses the import library to add entries to the import table for the EXE. 链接器使用导入库将条目添加到EXE的导入表中。 Which is then in turn used by Windows at runtime to figure out what DLLs need to be loaded to run the program. 然后Windows在运行时使用它来确定需要加载哪些DLL来运行程序。

In a static library, the lib file contains the actual object code for the functions provided by the library. 在静态库中,lib文件包含库提供的函数的实际对象代码。 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. 在共享版本(您称之为静态链接动态库)中,只有足够的代码可以在运行时建立动态链接。

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. 来晚了一点,但不,你没有链接.lib。 Well, you link to the lib with libraryloaderex in it. 好吧,你用libloaderex链接到lib。 But for the actual library you're using, you provide your own bindings via C function pointers and loadlibrary fills those in. 但对于您正在使用的实际库,您可以通过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

A lib files is read by the linker and a dll file is used during execution. 链接器读取lib文件,并在执行期间使用dll文件。 A lib file is essentially useless during execution and a linker is incapable of reading a dll file (except possibly in a manner irrelevant here). 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. 对于静态库,.lib文件包含obj文件。 Each obj file is the output of one and only one compiler source code input file. 每个obj文件是一个且只有一个编译器源代码输入文件的输出。 A lib file is just a collection of related obj files, much like putting obj files in a directory. lib文件只是相关obj文件的集合,就像将obj文件放在目录中一样。 That is essentially what a lib file is, a library of obj files. 这本质上是一个lib文件,一个obj文件库。 For a static link, all of the obj files that an executable uses are combined into one file. 对于静态链接,可执行文件使用的所有obj文件都合并到一个文件中。 Compare that to a dynamic link in which the executable is in a file separate from the other code it uses. 将其与动态链接进行比较,其中可执行文件位于与其使用的其他代码分开的文件中。

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. 为了实现动态链接,Microsoft修改了lib文件的使用,使得它们引用dll文件而不是obj文件中的位置。 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. 除了动态链接的lib文件指定dll文件之外,它们与它们中的信息完全相同。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM