/Mt,/MD compilation options, and problems that may cause memory application and release in different heaps

Source: Internet
Author: User

 

I. Differences between MD (D) and MT (d) compilation Options 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, indicating that the Runtime Library provides a DLL by the operating system,ProgramIs not integrated.

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

 

Ii./MD,/MT Selection 1. Why/MD? No/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 applying for a heap and releasing B heap.

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 each have a C Runtime LibraryCodeOr 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.

)

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 :) (2013.5.30 supplement)

Iv. 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

Appendix:

: Http://files.cnblogs.com/cswuyg/Test_MD_and_MT.rar

 

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.