开发者

Visual C++: What is a dynamically linked .lib file?

开发者 https://www.devze.com 2022-12-20 12:13 出处:网络
I noticed the follow开发者_如何学Going 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 noticed the follow开发者_如何学Going 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:

  1. Runtime Library option is set to Multi-threaded (Debug) DLL.
  2. Configuration Type is set to Static Library (.lib)

My confusion is:

  1. Isn't there a conflict in the library options above? (Static Library says one option, DLL says another)
  2. 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消