Reputation: 228
I am perplexed by the concept between the run-time library option on Windows. I thought I know the difference between lib and dll. The lib will be compiled to your code and the dll will be loaded dynamically on execution. But when I compile a lib, I am obliged to make a choice between /MD
and /MT
. On msdn.microsoft.com it is said that:
Applications compiled with /MD are statically linked to MSVCRT.lib. This library provides a layer of code that enables the linker to resolve external references. The actual working code is contained in MSVCRversionnumber.DLL, which must be available at run time to applications linked with MSVCRT.lib.
At this time I cannot catch the idea that when I using a lib I also need to dynamically load a dll. It seems like that using /MD
option to build a lib will result in a "dynamical lib", or using /MT
option to build a dll will result in a "static dll".
So what's the difference between "/MD
vs /MT
" and "lib vs dll"?
Upvotes: 3
Views: 2329
Reputation: 30605
A key aspect of this is differentiating between the build/compile time issues and the runtime issues.
At build time, the compiler and linker need sufficient information on how to compile and construct the code; data definitions, function locations etc. The header files provide most of that detail for the compiler, the linker needs to be able to locate the functions1 to complete the process.
If the dll is provided with a lib as well (as is the case with /MD
), this lib contains the minimum required code for the linker to find the functions required and some additional code to load the dll at runtime. A program can be made that does not link with a lib (even though there is a dll), you will then need to load the dll at runtime (via LoadLibrary
) and fix up the pointers (via GetProcAddress
) to the functions1 you need to call. With a C++ library that is difficult, hence it is generally not attempted, the name mangling make this harder (with a C library it is generally much easier).
If the lib is a static lib with no associated dll (for the runtime this is /MT
), then the lib contains all the code needed to run and execute its given functionality. The linker links all the required code up into the target and no additional runtime loading is required.
1 I loosely use the word functions here, but it includes all externals as well.
Upvotes: 4
Reputation: 106
In both cases the application will be statically linked with a .lib
file, but if /MT
is used then it will be linked with libcmt.lib
or libcmtd.lib
(Release/Debug), which contain the C run-time itself, whereas if /MD
is used it will be linked with msvcrt.lib
or msvcrtd.lib
, which are just import libraries for the DLL and do not contain the run-time.
See https://msdn.microsoft.com/en-us/library/abx4dbyh(v=vs.120).aspx for details.
(Note that in VS2015 the libraries have been split and have new names; the C99-portion names are libucrt.lib
, libucrtd.lib
, ucrt.lib
, and ucrtd.lib
, respectively.)
Upvotes: 2
Reputation: 206577
Every DLL
has a corresponding .lib
file too. The linker links against the .lib
file, which has the necessary instructions that indicates which symbols need to be loaded at run time from a DLL
.
Upvotes: 0