I. Differences between the MD (d) and MT (d) Compilation options
1. Location of compilation options
Take VS2005 as an example, open like this:
1) Open the Project's Property Pages dialog box
2) Click on the left-C + + section
3) Click on the Code Generation Section
4) Right line sixth Runtime Library project
2. The meaning of each setting option represents
Compilation options |
Contains |
Statically linked Lib |
Description |
/md |
_MT, _dll |
MSVCRT.lib |
Multithreaded, release, DLL versions of the run-time library |
/MDd |
_DEBUG, _MT, _dll |
MSVCRTD.lib |
Multithreaded, Debug, DLL versions of the run-time library |
/mt |
_mt |
LIBCMT.lib |
Multithreaded, release version of the run-time library |
/MTd |
_DEBUG, _MT |
LIBCMTD.lib |
multithreaded, debug version of the run-time library |
To put it simply:
(1)/MD, which indicates that the runtime library is provided by the operating system with a DLL that is not integrated in the program.
(2)/MT, which indicates that the runtime library is integrated by the program.
Second, the choice of/MD and/MT
1, why Choose/MD, not select/MT?
(1) The program does not need static link runtime library, can reduce the size of the software;
(2) All modules are used/MD, the same heap is used, there is no heap a application, b heap release problem;
(3) The user machine may be missing the dynamic runtime library that we compile with. (add: If we have more than one DLL in the software, the use of/MT volume increases too much, you can consider the/MD + own system runtime Library)
2, why choose/MT, do not choose/MD?
(1) Some systems may not have the version of the runtime library required by the program, and the program must link the runtime library statically.
(2) Reduce the dependence of the module on the outside world.
3, multiple modules, you must select the same runtime library.
Three, select/MT need to solve the problem of heap space release
Different modules have a C Runtime library code, or no C runtime library at all, resulting in each module having its own heap. If space is requested in the A heap, the release in the B heap will crash and the space requested in module A must be released in module A.
The DLL for the appendix and the Dlluser code, in the case of the STL string, verify the problem by modifying the compilation options. String at the time of assignment need to release the original memory space, and then request new memory space to store new content, if the cross-module, the release of "a module request B module release" Problem, causing the program to crash.
(The contents of the crash caused by freeing memory across modules are discussed in the fifth edition of Windows core programming Page511 talk about DLL and process address space)
Iv. Select/MD need to pay attention to the issue of multiple modules using different versions of the runtime library
(2012-9-17 Supplement)
(2013.5.30 Supplement)
Multiple DLLs are loaded by an EXE LoadLibrary, and if the runtime libraries used by these DLLs are different, the load failure may occur because the old version of the runtime library is already in use and a DLL requires a new version of the runtime library, and the old version does not meet the requirements.
If all the modules in the project are written by themselves or fully controlled, then this problem is not difficult to solve, only need to set/MD in the project settings, and then compile in the same environment once the line. But what if this module is available from outside?
This may be the case: a dynamic library uses a B static library, b Static library uses C dynamic library, B Static library is provided by the outside world, we want to use it, but cannot modify it, we can not touch the C dynamic library. If the run-time library version used by the C dynamic Library is inconsistent with the local use of the compiled a dynamic library, then the embedded information in a dynamic library will record two different versions of the runtime library, and it will be loaded with a new version. Suppose a dynamic library is loaded by an EXE LoadLibrary, and the runtime library of the EXE itself is old, which will cause a dynamic library to fail to load, even if the new runtime library is copied to the directory, because EXE has already loaded the old runtime library. You must use the manifest file to specify that the runtime library embedded in the A dynamic library be a version, ignoring the version of the runtime library used by the C dynamic library.
This is a complicated question. Verify that the PE file loading for Windows will take precedence over the runtime library, the runtime library's records in the static library .... Just keep in mind that the component versions used by the outside world try to avoid using/MD (which can cause bloat?). It is said that the installation package can do byte-stream compression.
Attach another question: Static library dependencies: Exe-->liba-->libb, now don't want to let EXE touch Libb, so liba Librarian option-->general option-->link Library Dependencies is set to Yes so that Liba will contain libb,exe only need to touch Liba. It is also important to note that Liba's reliance on LIBB is only required and can only be set in solution project dependencies, if it is written in Liba code "#pragma comment (lib," LibB.lib "), will cause exe in link Liba when prompted to find Liba. If the EXE also has a link error, it must be the VS Cramp:)
V. References
1, Microsoft on the MT, MD and other detailed introduction
Http://msdn.microsoft.com/en-us/library/2kzt1wy3 (v=vs.71). aspx
2, do not appear a module application, b module release of the situation
Http://www.cnblogs.com/minggoddess/archive/2010/12/15/1907179.html
3. What are the versions of the runtime library?
Http://www.cppblog.com/MichaelLiu/articles/10607.html
4. Discussion on the release of heap space on CSDN
Http://topic.csdn.net/t/20010112/09/57983.html
Http://topic.csdn.net/t/20031009/17/2338051.html
Http://topic.csdn.net/u/20090502/00/bf1602e3-ddf5-49b0-af81-8a23383f9ddc.html
http://blog.csdn.net/blz_wowar/article/details/2176536
5. Different stacks of different modules
Http://www.cnblogs.com/WengYB/archive/2011/08/18/2144727.html
6. Load failed sharing due to runtime library version issue
http://blog.csdn.net/dev_yarin/article/details/6768373
http://blog.163.com/henan_lujun/blog/static/19538333200611485511640/
7, "Windows core programming"
8. Windows heap with CRT heap
http://social.msdn.microsoft.com/Forums/vstudio/en-US/1e67edb1-9c2f-4f30-985a-7a0969366b1a/ Windows-heap-vs-crt-heap?forum=vcgeneral
Http://msdn.microsoft.com/en-us/library/ms810466.aspx
Appendix:
Demo Code: Http://files.cnblogs.com/cswuyg/Test_MD_and_MT.rar
/MT,/MD compilation options, and issues that may cause application and release of memory in different heaps