是否有不同类型的 .lib 文件?

Are there different types of .lib files?

我使用 Qt 编译了 minimal example 代码,并注意到 linking 到它的 .lib 文件添加了对我编译程序的要求 link到其对应的 .dll 文件。

我想自己创建一个 .lib 供我的其他项目之一使用,但不想同时创建 .dll 因为它必须 link到.


来自这个问题的答案:Difference between static and shared libraries?

Static libraries are .a (or in Windows .lib) files. All the code relating to the library is in this file, and it is directly linked into the program at compile time. A program using a static library takes copies of the code that it uses from the static library and makes it part of the program. [Windows also has .lib files which are used to reference .dll files, but they act the same way as the first one].

我对 .lib 文件有两种类型的理解是否正确:

如果这个观察是正确的,如何编译这些类型之一的 .lib

是的,从这个意义上讲,有两种类型的 .lib 文件。这是特定于 Windows(或者更确切地说,特定于 DLL)的。

在 Windows 上,静态库 是单个文件,通常扩展名为 .lib。链接静态库会将存储在其中的代码(目标文件)复制到您的可执行文件中。这相当于 Unix 世界的 .a 个文件。

另一方面,DLL(共享库)有两部分:包含代码的动态加载库本身(.dll),和一个 导入库 (.lib),其中包含某种 "stub code" 以满足 linker 依赖。您 link 针对导入库(DLL 随附的 .lib 文件),其中包括 DLL 函数的 "stub code",并且还将您的可执行文件标记为需要 DLL 在启动时加载.

在 Visual Studio 中,您可以 select 每个项目的项目类型:静态库(将生成 .lib 文件)或动态库(会产生.dll文件及其对应的.lib文件)。

在 Unix 世界中,它的工作方式不同:共享库(扩展名 .so)本身在 linking 期间使用,这会创建加载程序依赖项。