#visual studio# Runtime library MT, MTd, MD, MDd

In the development of the window program is often encountered when the compiled program is not able to run on another machine, which is generally caused by the installation of the corresponding runtime library on another machine, then this compile option MT What is the relationship between MTd, MD and MDd? This is the explanation of msdn above:

 

  • MT: mutithread, multi-threaded library, the compiler will select the multi-threaded static connection library from the runtime library to interpret the code in the program, that is, connect the LIBCMT.lib library.
  • MTd: mutithread+debug, multi-threaded debug version, connected to LIBMITD.lib library
  • MD: MT+DLL, multi-threaded dynamic library, connected to MSVCRT.lib library, this is an import library, corresponding to the dynamic library is MSVCRT.dll
  • MDd: MT+DLL+debug, multi-threaded dynamic debugging library, connected to MSVCRTD.lib library, corresponding to dynamic library is MSVCRTD.dll

When developing a multi-threaded program (single-threaded article is not discussed), you need to select one of MT, MTd, MD, MDd.

 

forMT/MTd, since the connection runtime library is LIBCMT.lib/LIBCMTD.lib, these two libraries are static libraries.Therefore, the program compiled in this way can be moved to another machine.

But forMD/MDd, connected to the dynamic librarySo if there is no other machineMSVCRT.dll/MSVCRTD.dllWhen you are prompted, the error is missing from the dynamic library.

 

I have made such a mistake and thought that it is compiled in MT/MTd mode. The program is statically linked to all the libraries. In fact, it is wrong. It can only decide whether the runtime library is dynamic or static, and the library written by the user or Other third-party libraries, depending on the code (explicitly connected to the dynamic library Loadlibrary) or the provided lib file (for import or static libraries), when you move the program to another machine, you still need to bring the required Dynamic library.

Looking at an example, compiling a static library and a dynamic library both implement the function of adding two integers:


// adds.h
 // add followed by s for static library
#pragma once
int add(int,int);
// adds.cpp
 // static library
#include "adds.h"
int add(int a, int b)
{
    return a+b;
}

 

Above, the runtime library selects MTd and compiles it into a static library adds.lib
 

// addd.h
// add followed by d to represent the dynamic library
#pragma once
#ifndef MYLIB_API
#define MYLIB_API _declspec(dllexport)
#endif
 
MYLIB_API int  add(int,int);
// addd.cpp
 // dynamic library
#include "addd.h"
int add(int a, int b)
{
  return a+b;
}

Above, the runtime library selects MTd and compiles it into the dynamic library addd.lib, addd.dll

// test.cpp
 // test program 
#include <iostream>
 
 / / Test the static library, here is 1, test the dynamic library, here is 0
#define TEST_STATIC_LINK 1
 
#if TEST_STATIC_LINK 
  #include <adds.h>
#else
  #define MYLIB_API _declspec(dllimport)
  #include "addd.h"
#endif
 
using namespace std;
 
int main()
{
    cout << add(2,3) << endl;
    return 0;
}

Test program compiled with MTd

  • 1. Test the static library, define TEST_STATIC_LINK as 1, provide adds.lib, generate executable file, move to another machine to run, because the test program and adds.lib both statically connect to the runtime library.
  • 2. Test the dynamic library, TEST_STATIC_LINK is defined as 0, provide addd.lib, generate executable file, move to another machine to run, but need addd.dll, because addd library static connection runtime library, test program static connection Runtime library, dynamically connect addd library

 

In the above example, the add library and the test program both select the MTd runtime library. If the inconsistency causes some compilation connection errors, the novice is not mindful.

For example, if you select MDd, the connection will have such an error:

1> is linking...

 1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) Already in LIBCMTD.lib(typinfo. Defined in obj)

 1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) Already in LIBCMTD. Defined in lib(typinfo.obj)

That is, a program that mixes different runtime libraries (static and dynamic, debug and non-debug) may cause conflicts, so the same runtime library should be used in a program.

Intelligent Recommendation

Openssl windows compile 64 bit mt mtd md mdd

reference: Openssl source download address:https://www.openssl.org/source/I used openssl-1.1.0i First install ActivePerl:https://www.activestate.com/activeperl/downloadsAfter loading it, if you start ...

Some links and problems between MTD/MT/MDD/MD and LIB/DLL

Where do you start talking about this topic? The blogger Xiaobai one, recently debugged the program (WIN-MSVC) and did not understand some basic problems, in line with the principle of meeting the pro...

Reproduced - dynamic, static compiler and MD, MDd, MT, MTd compilation

As these newcomers have do not understand, read the god of explanation sobering. Great God URL: https: //blog.csdn.net/u012273127/article/details/71419499 First, the problem of lead Recently a new in ...

[PE] Use and difference of VS compilation options MD, MDd, MT, MTd

###Date: 2017/10/23 1. Configuration of CRT compilation options Location of compilation options: Properties -> C/C++ -> Code Generation -> Runtime Library 2. Types of compilation options (1)/...

The difference between the compilation options /MT, /MTd, /MD, /MDd, etc.

Here is a summary of their differences. The ‘d’ at the back represents the DEBUG version, and the one without ‘d’ is the RELEASE version. First of all /MT /MT is "multithr...

More Recommendation

About the pit of the VS compilation connection. (/ MT) (/ MTD) (/ MD) (/ MDD)

  Microsoft gives the program that the runtime used by the default is (/ MD) (/ MDD) is a dynamic runtime. Sometimes it contains a third-party library, a link is a bunch of error, then run the li...

CMAKE Set MSVC Engineering MT/MTD/MD/MDD

Articles directory 0. Foreword 1. How to set up 1.1 Cmakelists code 1.2 Point 1: Policy 1.3 Key points 2: set_proprty 0. Foreword existMSVCEngineeringRight-click-> Properties,turn upConfiguration a...

libcef-compile operation mode-MTD/MT-MDD/MD

Article Directory 1. Open the project 2. Modify options 3. Verification test In an embedded running environment, most of the host software may use the MD mode to load and run the C++ runtime library. ...

The difference between multithreading (/MT), multithreaded debugging (/MTd), multithreaded DLL (/MD), multithreaded debugging DLL (/MDd) in VS

A language development environment often comes with a language library, which is a wrapper around the operating system's API. We also call these language libraries a runtime library. For the MSVC runt...

Understanding of /MT, /MTd, /MD, /MDd options when compiling static and dynamic link libraries for VC

Most people know that in the Windows development process, to achieve code reuse, it will be packaged into an interface, and then packaged into a library form, which has a static library and a dynamic ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top