Visual c ++ compiler options/MD,/ml,/mt,/LD

Source: Internet
Author: User

Some time ago, when compiling a program that references a self-written static library, there were always multiple redefinition errors during the link, and your code clearly did not redefine these things, for example:
Libcmt. Lib (_ file. OBJ): Error lnk2005: ___ initstdio already defined in libc. Lib (_ file. OBJ)
Libcmt. Lib (_ file. OBJ): Error lnk2005: ___ endstdio already defined in libc. Lib (_ file. OBJ)
Libcmt. Lib (_ file. OBJ): Error lnk2005: _ cflush already defined in libc. Lib (_ file. OBJ)
Libcmt. Lib (_ file. OBJ): Error lnk2005: _ iob already defined in libc. Lib (_ file. OBJ)
Libcmt. Lib (osfinfo. OBJ): Error lnk2005: _ alloc_osfhnd already defined in libc. Lib (osfinfo. OBJ)
Libcmt. Lib (osfinfo. OBJ): Error lnk2005: _ set_osfhnd already defined in libc. Lib (osfinfo. OBJ)
Libcmt. Lib (osfinfo. OBJ): Error lnk2005: _ free_osfhnd already defined in libc. Lib (osfinfo. OBJ)
Libcmt. Lib (osfinfo. OBJ): Error lnk2005: _ get_osfhandle already defined in libc. Lib (osfinfo. OBJ)
Libcmt. Lib (osfinfo. OBJ): Error lnk2005: _ open_osfhandle already defined in libc. Lib (osfinfo. OBJ)
Libcmt. Lib (tolower. OBJ): Error lnk2005: _ tolower already defined in libc. Lib (tolower. OBJ)
Libcmt. Lib (tolower. OBJ): Error lnk2005: _ tolower already defined in libc. Lib (tolower. OBJ)
And so on.

The preliminary estimation is about the compiler. By searching and viewing msdn on the internet, it turns out to be about the Single-thread or multi-thread runtime routines of Visual C ++ compiler options: my static library is in/ml single-threaded version during compilation, and the program that references it is in/MT multi-threaded version. They are respectively talking about libc during compilation. lib and libcmt. when Lib is connected to the Code, it is estimated that libc. lib and libcmt. lib is only the difference between a single thread and multiple threads, and the basic code is almost the same, so it will produce a re-definition error when the link is generated; then it will be okay to change the option/ml for compiling the static library to/mt.

Note:/MD is also a multi-threaded version. The user linked library to be applied must have the same compilation options as the application./MD and/MT may sometimes have errors, sometimes I did not. I tried this situation, but/MD and/ml seem to be okay./MT and/ml certainly have problems. It is unclear if there are other situations. If you are interested, please test it. ^ _ ^

If the code is used for multithreading, it is best to compile it into a multi-threaded version, otherwise unexpected problems may occur.

Compiler option settings (vc6): Project-> Settings-> C/C ++-> project options can be modified

Appendix:

The following describes the Visual C ++ compiler options in msdn:

These options select a single-threaded or multi-threaded runtime routine to indicate whether the multi-threaded module is a DLL, and select the release or debug version of The Runtime Library.

Option Description
/MD Definition_ MtAnd_ DLLThis allows you to select the multi-thread and DLL-specific versions of the runtime routine from the standard. h file at the same time. This option also enables the compiler to put the library name msvcrt. Lib into the. OBJ file.

The application compiled with this option is statically linked to msvcrt. Lib. This library provides a code layer that allows the linker to parse external references. The actual working code is included in msvcr71.dll. This library must be available for applications linked to msvcrt. Lib at runtime.

When _ static_cpplib (/d_static_cpplib) is defined and/MD is used, it causes the application to pass through the static multi-threaded Standard C ++ Library (libcpmt. lib) instead of the dynamic version (msvcprt. lib), and still use msvcrt. lib is dynamically linked to the master CRT.

/MDD Definition_ Debug,_ MtAnd_ DLLTo select the debugging multi-thread and DLL-specific versions of the runtime routine from the standard. h file. It also enables the compiler to put the library name msvcrtd. Lib into the. OBJ file.
/Ml Enable the compiler to put the library name libc. Lib into the. OBJ file so that the linker can use libc. lib to parse external symbols. This is the default operation of the compiler. Libc. Lib does not support multithreading.
/MLD Definition_ DebugThe compiler puts the library name libcd. Lib into the. OBJ file so that the linker can use libcd. lib to parse external symbols. Libcd. Lib does not support multithreading.
/Mt Definition_ MtTo select the multi-threaded version of the runtime routine from the Standard Header (. h) file. This option also enables the compiler to put the library name libcmt. Lib into the. OBJ file so that the linker can use libcmt. lib to parse external symbols. To create a multi-threaded program,/Mt or/MD (or their debugging equivalent options/MTD or/MDD) is required ).
/MTD Definition_ DebugAnd_ Mt. Definition_ MtThis will cause you to select a specific multi-threaded version of the runtime routine from the standard. h file. This option also enables the compiler to put the library name libcmtd. Lib into the. OBJ file so that the linker can use libcmtd. lib to parse external symbols. To create a multi-threaded program,/MTD or/MDD (or their non-debugging equivalent options/Mt or Md) is required ).
/LD Create a DLL.

Pass the/DLL option to the linker. Find the linkerDllmainFunction, but this function is not required. If noDllmainFunction. The linker insertsDllmainFunction.

Link DLL startup code.

If the Export (. exp) file is not specified on the command line, create the import/export (. Lib); link the import/export link to the application that calls your DLL.

Interpret/Fe as the name DLL rather than the. exe file. The default program name isBase name. Dll insteadBase name. EXE.

If one of the/m options is not explicitly specified, change the default Runtime Library support to/mt.

/LDD Create a debug DLL. Definition_ Debug.

WarningDo not mix the static and dynamic versions of the Runtime Library. There are multiple runtime database copies in a process, which may cause problems because the static data in the copies is not shared with other copies. The linker prohibits the use of both static and dynamic version links in the. exe file, but you can still use two (or more) Copies of the Runtime Library. For example, if it is linked to the Runtime library using the dynamic (DLL) version. when the EXE file is used together, the dynamic link library linked by the Runtime Library of the static (non-DLL) version may cause problems. (You should also avoid mixing the debug and non-DEBUG Versions of these libraries in a process ).

For more information about using the debug version of The Runtime Library, see the Runtime Library Reference.

The Knowledge Base Article q140584 also discusses how to select the appropriate C Runtime Library.

For more information about DLL, see DLL.

Set this compiler option in the Visual Studio development environment

  1. Open the properties page dialog box for this project. For more information, see Set Visual C ++ project properties.
  2. Click the "C/C ++" folder.
  3. Click the "Code Generation" property page.
  4. Modify the "RunTime Database" attribute.

Set this compiler option programmatically

See runtimelibrary attributes.

 

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.