Differences between MD (d) and MT (d) compilation options: mdmt

Source: Internet
Author: User

Differences between MD (d) and MT (d) compilation options: mdmt
1. Location of compilation options

Take VS2005 as an example:

1) Open the project's Property Pages dialog box

2) Click the C/C ++ section on the left.

3) Click the Code Generation section.

4) the sixth line of the Runtime Library Project on the right

2. Meaning of each setting Option

Compilation options

Include

Static link lib

Description

/MD

_ MT, _ DLL

MSVCRT. lib

Multi-thread, Release, DLL version Runtime Library

/MDd

_ DEBUG, _ MT, and _ DLL

MSVCRTD. lib

Multi-thread, Debug, DLL version Runtime Library

/MT

_ MT

LIBCMT. lib

Multi-thread, Release version Runtime Library

/MTd

_ DEBUG, _ MT

LIBCMTD. lib

Runtime Library of multi-thread and Debug Versions

To put it simply:

(1)/MD indicates that the Runtime Library provides a DLL by the operating system and is not integrated in the program.

(2)/MT, indicating that the Runtime Library is integrated by the program.

2. Selection of/MD and/MT

1. Why do I select/MD instead of/MT?

(1) The program does not need to statically link the Runtime Library, which can reduce the Software size;

(2) All modules adopt/MD and use the same heap. There is no issue of stack A application or stack B release;

(3) The user machine may lack the dynamic Runtime Library we use during compilation. (Supplement: if our software has multiple DLL and the/MT volume increases too much, you can consider/MD + Built-in System Runtime Library)

2. Why do I select/MT instead of/MD?

(1) Some systems may not have the Runtime Library of the version required by the program. The program must link the Runtime Library to the static link.

(2) reduce the external dependencies of modules.

3. You must select the same Runtime Library for multiple modules.

3. Select the heap space to be resolved by/MT

Different modules have their own C Runtime library code, or there is no C Runtime Library at all, resulting in each module having its own heap. If you apply for A space in heap A, releasing it in heap B will crash. The space applied for in module A must be released in module.

The DLL and DLLUser code in the appendix, taking STL string as an example, verify this problem by modifying the compilation options. When assigning values to a string, you need to release the original memory space and then apply for a new memory space to store new content, during the release, the program crashes due to the "Application for release of module A and Module B" issue.

(The contents that cause the crash caused by the release of memory Across modules are discussed in Page511 of windows core programming)

4. When selecting/MD, pay attention to the problem that multiple modules use different versions of runtime libraries.

)

(Supplement)

Multiple dll files are loaded by one exe LoadLibrary. If these dll files use different runtime libraries, the loading may fail because the earlier version of the Runtime Library is already in use, A dll requires a new version of the Runtime Library. The old version does not meet the requirements.

If all the modules in the project are self-written or fully controlled, it is not difficult to solve this problem. You only need to set/MD in the Project Settings, compile it once in the same environment. But what if this module is provided by the outside world?

This may happen: A dynamic library uses B static library, B static library uses C dynamic library, and B static library is provided by the outside world. We need to use it, but it cannot be modified, and we cannot access the C dynamic library. If the Runtime Library version used by the C dynamic library is inconsistent with that used locally to compile the dynamic library, the embedded information in the dynamic library records the runtime libraries of two different versions, when it is loaded, it may select a new version. Assume that A dynamic library is loaded by an exe LoadLibrary, And the Runtime Library of this exe is old. This will cause A dynamic library loading failure, even if you copy the new Runtime Library to the directory, it does not work because the exe process has loaded the old Runtime Library. At this time, you must use the manifest file to specify A version of the Runtime Library embedded in the dynamic library, ignoring the version of the Runtime library used by the C dynamic library.

This problem is quite complicated. I didn't try to verify what kind of priority the PE file loading on windows will make to the Runtime Library, and the records of the Runtime Library in the static library .... Remember to avoid using/MD for components used by the outside world (will this lead to expansion? It is said that the installation package can perform byte stream compression ).

Another problem is attached: dependency between the static library: exe --> libA --> libB. Now, I don't want the exe to be exposed to libB, set the librarian option --> General option --> Link Library Dependencies of libA to Yes, so that libA contains libB, and exe only needs to access libA.Note that the libA dependency on libB only needs to be set in the Dependencies of the Solution Project. If "# pragma comment (lib," libB..If the exe still has a link error, it must be VS cramps :)

V. References

1. Microsoft's detailed introduction to MT and MD

Http://msdn.microsoft.com/en-us/library/2kzt1wy3 (v = VS.71). aspx

2. do not apply for module A or release Module B.

Http://www.cnblogs.com/minggoddess/archive/2010/12/15/1907179.html

3. Versions of the runtime Database

Http://www.cppblog.com/MichaelLiu/articles/10607.html

4. Discussion on heap space release 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. Sharing failed due to library version issues during running time

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 and 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.