为什么 visual studio 需要静态库 (.lib) 来进行动态链接?

Why visual studio needs a static library (.lib) for dynamic linking?

我想在我的项目中使用一些 dll ( VS2013 - c++ ),

我使用"project->properties->vc++ directories"给出了dll和headers的路径,在构建项目后,出现链接器错误(常见"unresolved external symbol")。将.lib文件交给链接器即可解决!

但是为什么动态链接需要静态库(.lib)???

  • 告诉它DLL的名称和里面的内容,如果使用的话入口点号是多少,

  • 将二进制接口(.lib)与其实现(DLL)隔离开来。例如,Winsock 有一个标准的 .lib 文件,它独立于当时的实际多供应商实现。

它是一个导入库,其中包含导出的定义,位于 DLLDLL 的名称 (*).

You can use LIB with the /DEF option to create an import library and an export file. LINK uses the export file to build a program that contains exports (usually a dynamic-link library (DLL)), and it uses the import library to resolve references to those exports in other programs.

还有:

In most situations, you do not need to use LIB to create your import library. When you link a program (either an executable file or a DLL) that contains exports, LINK automatically creates an import library that describes the exports. Later, when you link a program that references those exports, you specify the import library.

动态库在运行时加载(在应用程序启动时)- 链接器不检查某些特定符号在 DLL 中的位置。 Header 表示 __dllimport - "this symbol is an extern, it should be imported from somewhere"。 Lib 说 "I know where this symbol is - it resides in XXX.dll, so look there after startup"。


(*) 我看到很多人试图更改 .lib 和相应的 .dll 的名称,并期望它会工作。 .lib 的内容是它没有的原因。