Qt sets the runtime library MT, MTd, MD, MDd in pro, only suitable for the VS version of Qt

tags: qt  visual studio  Library

Transfer from: http://blog.csdn.net/caoshangpa/article/details/51416077


1. Set the runtime library in pro

Recently, the following error occurred when using Qt5.6.0 (VS2013 version) to call a static library of Debug version compiled with Visual Studio 2013:

Error: LNK2038: Detected mismatch of "RuntimeLibrary": The value "MTd_StaticDebug" does not match the value "MDd_DynamicDebug" (in widget.obj)

It can be seen from this error that the Debug version Qt5.6.0 library is compiled from the Qt source code through Visual Studio 2013, and the runtime library at compile time is set to MDd. But when the called static library is compiled by Visual Studio 2013, the runtime library is set to MTd. To avoid this error, you can only set the two runtime libraries to be the same at compile time.

Visual Studio can set the runtime library in the properties, as shown below:


So how to set the runtime library in the pro file? It can be set by the following four qmake variables.

QMAKE_CFLAGS_DEBUG

This variable contains the flags for the C compiler in debug mode

QMAKE_CFLAGS_RELEASE

This variable contains the compiler flags for creating a non-debuggable application

QMAKE_CXXFLAGS_DEBUG

This variable contains the C++ compiler flags for creating a debuggable application

QMAKE_CXXFLAGS_RELEASE

This variable contains the C++ compiler flags for creating an application


The above called library can also be compiled with Qt5.6.0. At this time, the following two lines need to be added to the pro file.
[cpp] view plain copy
  1. QMAKE_CFLAGS_DEBUG += -MDd  
  2. QMAKE_CXXFLAGS_DEBUG += -MDd  
In this case, when the library is called again, no error will be reported.
two.Visual Studio runtime library MT, MTd, MD, MDd research

When developing a window program, we often encounter situations where the compiled program cannot be run on another machine. This is usually caused by the fact that the corresponding runtime library is not installed on the other machine. Then this is related to the compilation option MT. What is the relationship between MTd, MD, and MDd? This is the explanation from msdn above:

MT: mutithread, multi-threaded library, the compiler will select a multi-threaded static link library from the runtime library to interpret the code in the program, that is, link to the LIBCMT.lib library

MTd: mutithread+debug, multi-threaded debugging version, connected to LIBMITD.lib library

MD: MT+DLL, multi-threaded dynamic library, link to MSVCRT.lib library, this is an import library, the corresponding dynamic library is MSVCRT.dll

MDd: MT+DLL+debug, multi-threaded dynamic debugging library, connected to MSVCRTD.lib library, corresponding dynamic library is MSVCRTD.dll

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


For MT/MTd, since the link runtime library is LIBCMT.lib/LIBCMTD.lib, these two libraries are static libraries, so the program compiled in this way can also run normally when moved to another machine.

But for MD/MDd, the dynamic library is connected, so if there is no MSVCRT.dll/MSVCRTD.dll on another machine, it will prompt the error of missing dynamic library.

 

I once made such a mistake, thinking that when compiling in MT/MTd mode, the program is statically linked to all libraries. In fact, it is wrong. It can only determine whether the runtime library is dynamically linked or statically linked. For libraries written by users or For other third-party libraries, the connection method depends on the code (explicitly connect the dynamic library Loadlibrary) or the provided lib file (import library or static library). When moving the program to another machine, you still have to bring what you need Dynamic library.

Let's look at an example, compile a static library and a dynamic library, both of which realize the function of adding two integers:

[cpp] view plain copy
  1. // adds.h  
  2. // Add an s after add for static library  
  3. #pragma once  
  4. int add(int,int);  
[cpp] view plain copy
 
 
  1. // adds.cpp  
  2. // Static library  
  3. #include "adds.h"  
  4. int add(int a, int b)  
  5. {  
  6.     return a+b;  
  7. }  
  8. Above, select MTd for runtime library and compile it into static library adds.lib
[cpp] view plain copy
 
 
  1. // addd.h  
  2. // Add d after add to represent dynamic library  
  3. #pragma once  
  4. #ifndef MYLIB_API  
  5. #define MYLIB_API _declspec(dllexport)  
  6. #endif  
  7.   
  8. MYLIB_API int  add(int,int);  
[cpp] view plain copy
 
 
  1. // addd.cpp  
  2. // Dynamic library  
  3. #include "addd.h"  
  4. int add(int a, int b)  
  5. {  
  6.   return a+b;  
  7. } Above, select MTd for runtime library and compile it into dynamic library addd.lib, addd.dll
[cpp] view plain copy
 
 
  1. // test.cpp  
  2. // test program  
  3. #include <iostream>  
  4.   
  5. // Test the static library, here is 1, and test the dynamic library, here is 0  
  6. #define TEST_STATIC_LINK 1  
  7.   
  8. #if TEST_STATIC_LINK   
  9.   #include <adds.h>  
  10. #else  
  11.   #define MYLIB_API _declspec(dllimport)  
  12.   #include "addd.h"  
  13. #endif  
  14.   
  15. using namespace std;  
  16.   
  17. int main()  
  18. {  
  19.     cout << add(2,3) << endl;  
  20.     return 0;  
  21. }The test program is compiled with MTd

1. Test static library, TEST_STATIC_LINK is defined as 1, provide adds.lib, generate executable file, move to another machine to run, because test program and adds.lib are statically connected to runtime library

2. Test 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 statically connects to runtime library, test program statically connects Run-time library, dynamically link addd library

In the above example, both the add library and the test program select the MTd runtime library. If they are inconsistent, it will cause some compilation and connection errors, which will make novices confused.

For example, if adds select MDd, the connection will have this error:

1>Linking...

1>MSVCRTD.lib(ti_inst.obj) : error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) has been defined in LIBCMTD.lib(typinfo.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) has been defined in LIBCMTD.lib(typinfo.obj)

That is, if different runtime libraries (static library and dynamic library, debugging library and non-debugging library) are mixed in a program, conflicts may occur, so the same runtime library should be used in a program.


Reference link: http://blog.csdn.net/ybxuwei/article/details/9095067
Reference link: https://doc.qt.io/archives/4.6/qmake-variable-reference.html

Intelligent Recommendation

[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)/...

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

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

MT, MTd, MD, MDd explain MSVCRTD.LIB and LIBCMTD.LIB conflicts

Transfer from: The project development process encountered MSVCRTD.LIB and LIBCMTD.LIB conflicts, and later found the reason is that the code compiler compile c / c + + runtime version is not the same...

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

More Recommendation

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

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

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

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

Top