Visual C + + compiler options/MD,/ML,/MT,/LD

Source: Internet
Author: User
Tags knowledge base

Some time ago, when compiling a program that references a static library of its own writing, there are many redefined errors when linking, and your code clearly does not redefine these things, such as:
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)
Wait a minute.

So the initial estimate is a compiler problem, by searching online and viewing MSDN, the problem with the Visual C + + compiler option about single-threaded or multithreaded run-time routines: My static library compiled with/ML single-threaded version, and the program referencing it is/MT multithreaded version, they are compiled separately speaking LIBC.lib and LIBCMT.lib are connected to their own code, estimating that LIBC.lib and LIBCMT.lib are just the difference between single-threaded and multi-threaded, with the same basic code, so they create a link-time redefinition error, and then change the option of compiling the static library/ml to/MT.

Note that:/MD is also multi-threaded version, the application of the user link library and the application has the same compilation options,/MD and/MT sometimes error, sometimes not, I tried this situation, and/MD and/ml seems to be no problem;/MT and/ml are sure to be problematic. There is no other situation is not clear, interested can be tested, ^_^

If the code is for multithreading, it is best to compile into a multithreaded version, otherwise there may be some unexpected problems.

compiler option settings (VC6): Engineering Options--

Report:

The following is a description of MSDN's Visual C + + Compiler options:

These options select a single-threaded or multithreaded run-time routine, indicate whether a multi-threaded module is a DLL, and select a release or debug version of the runtime library.

Options Description
/md Define _MT and _dll to select multithreaded specific versions and DLL-specific versions of the runtime routines from the standard. h file at the same time. This option also causes the compiler to put the library name MSVCRT.lib into the. obj file.

Applications compiled with this option are statically linked to MSVCRT.lib. The library provides a layer of code that allows the linker to resolve external references. The actual work code is contained in the MSVCR71. DLL, the library must be available at run time for applications that are linked to MSVCRT.lib.

When using/MD with the _static_cpplib (/d_static_cpplib) defined, it causes the application to link through the static multithreaded standard C + + library (libcpmt.lib) rather than the dynamic version (MSVCPRT.lib) while still MSVCRT.lib dynamically linked to the main CRT.

/MDd Define _debug,_mt , and _dllto select Debug multithreaded specific versions and DLL-specific versions of the runtime routines from the standard. h file. It also causes the compiler to put the library name MSVCRTD.lib into the. obj file.
/ml Causes the compiler to put the library name LIBC.lib into the. obj file so that the linker can use LIBC.lib to resolve external symbols. This is the default action for the compiler. LIBC.lib does not provide multithreading support.
/MLd Defines the _DEBUG and causes the compiler to put the library name LIBCD.lib into the. obj file so that the linker uses LIBCD.LIB to resolve the external symbol. LIBCD.lib does not provide multithreading support.
/mt Define _MTto select a multithreaded specific version of the run-time routine from the standard header (. h) file. This option also causes the compiler to put the library name LIBCMT.lib into the. obj file so that the linker can use LIBCMT.lib to resolve external symbols. Creating multithreaded programs requires/MT or/MD (or their debug equivalent option/MTd or/MDD).
/MTd Define _DEBUG and _MT. Defining _MT causes a multithreaded specific version of the run-time routine to be selected from the standard. h file. This option also causes the compiler to put the library name LIBCMTD.lib into the. obj file so that the linker can use LIBCMTD.lib to resolve external symbols. Creating multithreaded programs requires/MTD or/MDD (or their non-debug equivalent option/MT or MD).
/ld Creates a DLL.

Pass the/dll option to the linker. The linker looks for the DllMain function, but it does not need the function. If you do not write the DllMain function, the linker inserts a DllMain function that returns TRUE.

Link DLL startup code.

If an export (. exp) file is not specified on the command line, an import library (. lib) is created, and the import library is linked to the application that called your DLL.

Interpret/fe as a named DLL instead of an. exe file; The default program name becomes the base name . dll instead of the base name . exe.

If one of the/M options has not been explicitly specified, the default run-time library support is changed to/MT.

/ldd Create a debug DLL. Define _DEBUG.
Warning do not mix static and dynamic versions of the run-time library. Having multiple copies of a run-time library in one process can cause problems because static data in the replica is not shared with other replicas. The linker prohibits both a static version and a dynamic version link within an. exe file, but you can still use two (or more) copies of the run-time library. For example, when used with an. exe file that is linked with a dynamic (DLL) version of the run-time library, a dynamic-link library that is linked with a static (non-DLL) version of the run-time library can cause problems. (You should also avoid mixing the debug and non-debug versions of these libraries in one process.)

For more information about using the debug version of the run-time library, see run-Time Library reference.

Knowledge Base article Q140584 also discusses how to select the appropriate C run-time library.

For further discussion of DLLs, see DLLs.

Setting this compiler option in the Visual Studio development environment

    1. Open the Property Pages dialog box for this project. For more information, see Setting Visual C + + project properties.
    2. Click the C + + folder.
    3. Click the Code Generation property page.
    4. Modify the run-time library property.

To set this compiler option programmatically

See RuntimeLibrary properties.

Visual C + + compiler options/MD,/ML,/MT,/LD

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.