Visual C++:什么是动态链接的 .lib 文件?
我注意到有关我使用的库的以下信息:
- 库被编译为 .lib 文件。
- 我的代码需要编译为多线程(调试)DLL 才能链接到该库。
我打开库的 .sln(解决方案)文件(它是开源的),并在其项目属性中看到以下内容:
- 运行时库选项设置为多线程(调试)DLL。
- 配置类型设置为静态库(.lib)
我的困惑是:
- 上面的库选项是否有冲突? (静态库表示一个选项,DLL 表示另一个选项)
- 动态链接的.lib是一种什么样的动物?它与 DLL 有什么不同?
请注意,我知道 Linux 世界中静态库和动态库之间的区别。
I noticed the following about a library I use:
- Library is compiled to .lib file.
- My code needs to be compiled as Multi-threaded (Debug) DLL to link to this library.
I open the .sln (solution) file of the library (it is open source) and see the following in its Project properties:
- Runtime Library option is set to Multi-threaded (Debug) DLL.
- Configuration Type is set to Static Library (.lib)
My confusion is:
- Isn't there a conflict in the library options above? (Static Library says one option, DLL says another)
- What kind of an animal is a .lib that is dynamically linked? How is it different from a DLL?
Note that I am aware of the difference between static libraries and dynamic libraries in the Linux world.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“运行时库”选项与您的库无关。它告诉编译器您将在运行时从
MSVCRTxx.DLL
导入函数。“配置类型”选项确实引用了您的库,因此独立于“运行时库”选项。
The "RunTime Library" option isn't about YOUR library. It tells the compiler that you'll import your functions from
MSVCRTxx.DLL
at runtime.The "configuration Type" option does refer to your library, and therefore is independent of the "RunTime Library" option.
Windows DLL 可以使用 LoadLibrary (或 LoadLibraryEx)API,但是您必须使用 GetProcAddress 或 GetProcAddressEx。您最好确保函数签名正确,否则就会像往常一样发生不好的事情。
LIB 文件允许 Windows 在 EXE 启动时为您执行所有操作(包括查找要使用的 DLL,以及递归加载依赖的 DLL),在运行时静态链接动态库,同时避免可执行代码使 EXE 文件膨胀。 ,并允许多个进程共享内存中的同一个 DLL 映像。
A Windows DLL can be dynamically loaded with the LoadLibrary (or LoadLibraryEx) API, but then you have to find and bind each exported function to a function pointer using GetProcAddress or GetProcAddressEx. You'd better get the function signatures right, or Bad Things Will Happen, as usual.
A LIB file allows Windows to do all that for you when your EXE is started (including finding which DLL to use, and recursively loading dependent DLL's), linking the dynamic library statically at run time, while avoiding bloating your EXE file with the executable code, and allowing several processes to share the same DLL image in memory.
我不知道配置不匹配,但是使用 .DLL 库创建的 .LIB 文件是一个“导出库” - 它不包含任何代码,而只包含 DLL 中可调用函数和对象的名称。链接器使用它来满足链接时的引用,这些引用最终通过运行时的动态加载来解析。
I don't know about the config mismatch, but a .LIB file when created with a .DLL library is an "export library" - it doesn't contain any code, but just the names of the callable functions and objects in the DLL. The linker uses this to satisfy references at link time which are finally resolved by dynamic loading at run-time.