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

时间:2022-06-25 13:19:37

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文件。那是对的吗?

5 个解决方案

#1


103  

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 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. 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中函数或数据项的地址。然后,必须将返回的地址强制转换为适当的指向函数的指针才能使用它。

#2


10  

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. 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.

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

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.

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

#3


6  

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。好吧,你用libloaderex链接到lib。但对于您正在使用的实际库,您可以通过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

#4


4  

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 reading a 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文件之外,它们与它们中的信息完全相同。

#5


1  

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导出一个/很多功能。

#1


103  

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 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. 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中函数或数据项的地址。然后,必须将返回的地址强制转换为适当的指向函数的指针才能使用它。

#2


10  

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. 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.

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

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.

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

#3


6  

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。好吧,你用libloaderex链接到lib。但对于您正在使用的实际库,您可以通过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

#4


4  

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 reading a 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文件之外,它们与它们中的信息完全相同。

#5


1  

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导出一个/很多功能。