C++ 检测到“RuntimeLibrary”不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14714877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mismatch Detected for 'RuntimeLibrary'
提问by Momonga
I downloaded and extracted Crypto++ in C:\cryptopp. I used Visual Studio Express 2012 to build all the projects inside (as instructed in readme), and everything was built successfully. Then I made a test project in some other folder and added cryptolib as a dependency. After that, I added the include path so I can easily include all the headers. When I tried to compile, I got an error about unresolved symbols.
我在 C:\cryptopp 中下载并解压了 Crypto++。我使用 Visual Studio Express 2012 来构建里面的所有项目(按照 readme 中的说明),一切都构建成功。然后我在其他文件夹中创建了一个测试项目,并添加了 cryptolib 作为依赖项。之后,我添加了包含路径,以便我可以轻松包含所有标题。当我尝试编译时,我收到有关未解析符号的错误。
To remedy that, I added C:\cryptopp\Win32\Output\Debug\cryptlib.lib
to link additional dependencies. Now I get this error:
为了解决这个问题,我添加C:\cryptopp\Win32\Output\Debug\cryptlib.lib
了链接其他依赖项。现在我收到这个错误:
Error 1 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(cryptlib.obj) CryptoTest
Error 2 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(iterhash.obj) CryptoTest
Error 3 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(sha.obj) CryptoTest
Error 4 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(pch.obj) CryptoTest
Error 5 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(misc.obj) CryptoTest
Error 6 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(queue.obj) CryptoTest
Error 7 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(algparam.obj) CryptoTest
Error 8 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(filters.obj) CryptoTest
Error 9 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(fips140.obj) CryptoTest
Error 10 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(cpu.obj) CryptoTest
Error 11 error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MDd_DynamicDebug' in program.obj C:\Data\Work\C++ VS\CryptoTest\CryptoTest\cryptlib.lib(mqueue.obj) CryptoTest
I also get:
我也得到:
Error 12 error LNK2005: "public: __thiscall std::_Container_base12::_Container_base12(void)" (??0_Container_base12@std@@QAE@XZ) already defined in cryptlib.lib(cryptlib.obj) C:\Data\Work\C++ VS\CryptoTest\CryptoTest\msvcprtd.lib(MSVCP110D.dll) CryptoTest
Error 13 error LNK2005: "public: __thiscall std::_Container_base12::~_Container_base12(void)" (??1_Container_base12@std@@QAE@XZ) already defined in cryptlib.lib(cryptlib.obj) C:\Data\Work\C++ VS\CryptoTest\CryptoTest\msvcprtd.lib(MSVCP110D.dll) CryptoTest
Error 14 error LNK2005: "public: void __thiscall std::_Container_base12::_Orphan_all(void)" (?_Orphan_all@_Container_base12@std@@QAEXXZ) already defined in cryptlib.lib(cryptlib.obj) C:\Data\Work\C++ VS\CryptoTest\CryptoTest\msvcprtd.lib(MSVCP110D.dll) CryptoTest
Error 15 error LNK2005: "public: __thiscall std::locale::id::id(unsigned int)" (??0id@locale@std@@QAE@I@Z) already defined in cryptlib.lib(iterhash.obj) C:\Data\Work\C++ VS\CryptoTest\CryptoTest\msvcprtd.lib(MSVCP110D.dll) CryptoTest
Warning 16 warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library C:\Data\Work\C++ VS\CryptoTest\CryptoTest\LINK CryptoTest
Error 17 error LNK1169: one or more multiply defined symbols found C:\Data\Work\C++ VS\CryptoTest\Debug\CryptoTest.exe 1 1 CryptoTest
The code I tried to compile was simple (I got this from another site):
我试图编译的代码很简单(我从另一个站点得到了这个):
#include <iostream>
#include <string>
#include "sha.h"
#include "hex.h"
using namespace std;
string SHA256(string data) {
byte const* pbData = (byte*) data.data();
unsigned int nDataLen = data.size();
byte abDigest[32];
CryptoPP::SHA256().CalculateDigest(abDigest, pbData, nDataLen);
return string((char*)abDigest);
}
int main(void) {
return 0;
}
Any ideas how to fix this? I really only need SHA-256 right now, nothing else. I am using Windows 7 64 bit, and I downloaded VS C++ today, so it should be the newest version.
任何想法如何解决这一问题?我现在真的只需要 SHA-256,没有别的。我使用的是 Windows 7 64 位,我今天下载了 VS C++,所以它应该是最新版本。
回答by yzt
(This is already answered in comments, but since it lacks an actual answer, I'm writing this.)
(这已经在评论中回答了,但由于它缺乏实际答案,我正在写这个。)
This problem arises in newer versions of Visual C++ (the older versions usually just silently linked the program and it would crash and burn at run time.) It means that some of the libraries you are linking with your program (or even some of the source files inside your program itself) are using different versions of the CRT (the C RunTime library.)
这个问题出现在较新版本的 Visual C++ 中(旧版本通常只是默默地链接程序,它会在运行时崩溃和烧毁。)这意味着您与程序链接的某些库(甚至某些源程序本身中的文件)正在使用不同版本的 CRT(C 运行时库。)
To correct this error, you need to go into your Project Properties
(and/or those of the libraries you are using,) then into C/C++
, then Code Generation
, and check the value of Runtime Library
; this should be exactly the same for allthe files and libraries you are linking together. (The rules are a little more relaxed for linking with DLLs, but I'm not going to go into the "why" and into more details here.)
要纠正此错误,您需要进入您的Project Properties
(和/或您正在使用的库),然后进入C/C++
,然后Code Generation
,并检查Runtime Library
; 对于您链接在一起的所有文件和库,这应该完全相同。(与 DLL 链接的规则稍微宽松一些,但我不会在这里讨论“为什么”和更多细节。)
There are currently four options for this setting:
此设置目前有四个选项:
- Multithreaded Debug
- Multithreaded Debug DLL
- Multithreaded Release
- Multithreaded Release DLL
- 多线程调试
- 多线程调试DLL
- 多线程发布
- 多线程发布DLL
Your particular problem seems to stem from you linking a library built with "Multithreaded Debug" (i.e. static multithreaded debug CRT) against a program that is being built using the "Multithreaded Debug DLL" setting (i.e. dynamic multithreaded debug CRT.) You should change this setting either in the library, or in your program. For now, I suggest changing this in your program.
您的特定问题似乎源于您将使用“多线程调试”(即静态多线程调试 CRT)构建的库与使用“多线程调试DLL”设置(即动态多线程调试 CRT)构建的程序相链接。您应该更改此设置在库中或您的程序中。现在,我建议在您的程序中更改此设置。
Note that since Visual Studio projects use different sets of project settings for debug and release builds (and 32/64-bit builds) you should make sure the settings match in all of these project configurations.
请注意,由于 Visual Studio 项目对调试和发布版本(以及 32/64 位版本)使用不同的项目设置集,因此您应该确保所有这些项目配置中的设置都匹配。
For (some) more information, you can see these (linked from a comment above):
有关(一些)更多信息,您可以看到这些(从上面的评论链接):
- Linker Tools Warning LNK4098on MSDN
- /MD, /ML, /MT, /LD (Use Run-Time Library)on MSDN
- Build errors with VC11 Beta - mixing MTd libs with MDd exes fail to linkon Bugzilla@Mozilla
- MSDN 上的链接器工具警告 LNK4098
- MSDN 上的/MD、/ML、/MT、/LD(使用运行时库)
- 使用 VC11 Beta 构建错误 - 将 MTd 库与 MDd exe 混合无法在 Bugzilla@Mozilla 上链接
UPDATE: (This is in response to a comment that asks for the reason that this much care must be taken.)
更新:(这是对评论的回应,该评论要求必须非常小心的原因。)
If two pieces of code that we are linking together are themselves linking against and using the standard library, then the standard library must be the same for both of them, unless greatcare is taken about how our two code pieces interact and pass around data. Generally, I would say that for almost all situations just use the exact same version of the standard library runtime (regarding debug/release, threads, and obviously the version of Visual C++, among other things like iterator debugging, etc.)
如果我们链接在一起的两段代码本身链接并使用标准库,那么标准库对它们来说必须是相同的,除非非常注意我们的两段代码如何交互和传递数据。一般来说,我会说,对于几乎所有情况,只需使用标准库运行时的完全相同版本(关于调试/发布、线程,显然还有 Visual C++ 的版本,以及迭代器调试等其他内容)
The most important part of the problem is this: having the same idea about the size of objects on either side of a function call.
问题最重要的部分是:对函数调用两侧的对象大小有相同的想法。
Consider for example that the above two pieces of code are called A
and B
. A is compiledagainst one version of the standard library, and B against another. In A's view, some random object that a standard function returns to it (e.g. a block of memory or an iterator or a FILE
object or whatever) has some specific size and layout (remember that structure layout is determined and fixed at compile time in C/C++.) For any of several reasons, B's idea of the size/layout of the same objects is different (it can be because of additional debug information, natural evolution of data structures over time, etc.)
例如,考虑上面的两段代码被称为A
和B
。A 是针对一个版本的标准库编译的,而 B 是针对另一个版本的。在 A 看来,标准函数返回给它的一些随机对象(例如一块内存或一个迭代器或一个FILE
对象或其他任何东西)具有一些特定的大小和布局(请记住,结构布局在 C/ C++。)出于多种原因中的任何一个,B 对相同对象的大小/布局的想法是不同的(可能是因为额外的调试信息、数据结构随时间的自然演变等)
Now, if A calls the standard library and gets an object back, then passes that object to B, and B touches that object in any way, chances are that B will mess that object up (e.g. write the wrong field, or past the end of it, etc.)
现在,如果 A 调用标准库并取回一个对象,然后将该对象传递给 B,并且 B 以任何方式接触该对象,则 B 很可能会弄乱该对象(例如,写入错误的字段,或越过结尾)等等)
The above isn't the only kind of problems that can happen. Internal global or static objects in the standard library can cause problems too. And there are more obscure classes of problems as well.
以上并不是唯一可能发生的问题。标准库中的内部全局或静态对象也可能导致问题。并且还有更模糊的问题类别。
All this gets weirder in some aspects when using DLLs (dynamic runtime library) instead of libs (static runtime library.)
当使用 DLL(动态运行时库)而不是 libs(静态运行时库)时,所有这些在某些方面都会变得更奇怪。
This situation can apply to any library used by two pieces of code that work together, but the standard library gets used by most (if not almost all) programs, and that increases the chances of clash.
这种情况适用于一起工作的两段代码使用的任何库,但标准库被大多数(如果不是几乎所有)程序使用,这增加了冲突的机会。
What I've described is obviously a watered down and simplified version of the actual mess that awaits you if you mix library versions. I hope that it gives you an idea of why you shouldn't do it!
如果您混合库版本,我所描述的显然是等待您的实际混乱的淡化和简化版本。我希望它能让您了解为什么不应该这样做!
回答by jww
I downloaded and extracted Crypto++ in C:\cryptopp. I used Visual Studio Express 2012 to build all the projects inside (as instructed in readme), and everything was built successfully. Then I made a test project in some other folder and added cryptolib as a dependency.
我在 C:\cryptopp 中下载并解压了 Crypto++。我使用 Visual Studio Express 2012 来构建里面的所有项目(按照 readme 中的说明),一切都构建成功。然后我在其他文件夹中创建了一个测试项目,并添加了 cryptolib 作为依赖项。
The conversion was probably not successful. The only thing that was successful was the running of VCUpgrade. The actual conversion itself failed but you don't know until you experience the errors you are seeing. For some of the details, see Visual Studioon the Crypto++ wiki.
转换可能不成功。唯一成功的是运行 VCUpgrade。实际转换本身失败了,但直到您遇到所看到的错误时,您才会知道。有关一些详细信息,请参阅Crypto++ wiki 上的Visual Studio。
Any ideas how to fix this?
任何想法如何解决这一问题?
To resolve your issues, you should download vs2010.zip
if you want static C/C++ runtime linking (/MT
or /MTd
), or vs2010-dynamic.zip
if you want dynamic C/C++ runtime linking (/MT
or /MTd
). Both fix the latent, silent failures produced by VCUpgrade.
要解决您的问题,vs2010.zip
如果您想要静态 C/C++ 运行时链接(/MT
或/MTd
),或者vs2010-dynamic.zip
如果您想要动态 C/C++ 运行时链接(/MT
或/MTd
),您应该下载。两者都修复了 VCUpgrade 产生的潜在的、无声的故障。
vs2010.zip
, vs2010-dynamic.zip
and vs2005-dynamic.zip
are built from the latest GitHub sources. As of this writing (JUN 1 2016), that's effectively pre-Crypto++ 5.6.4. If you are using the ZIP files with a down level Crypto++, like 5.6.2 or 5.6.3, then you will run into minor problems.
vs2010.zip
,vs2010-dynamic.zip
并且vs2005-dynamic.zip
是从最新的 GitHub 源代码构建的。在撰写本文时(2016 年 6 月 1 日),这实际上是 Crypto++ 5.6.4 之前的版本。如果您使用带有较低级别 Crypto++(例如 5.6.2 或 5.6.3)的 ZIP 文件,那么您会遇到一些小问题。
There are two minor problems I am aware. First is a rename of bench.cpp
to bench1.cpp
. Its error is either:
我知道有两个小问题。首先是对bench.cpp
tobench1.cpp
的重命名。它的错误是:
C1083: Cannot open source file: 'bench1.cpp': No such file or directory
LNK2001: unresolved external symbol "void __cdecl OutputResultOperations(char const *,char const *,bool,unsigned long,double)" (?OutputResultOperations@@YAXPBD0_NKN@Z)
C1083: Cannot open source file: 'bench1.cpp': No such file or directory
LNK2001: unresolved external symbol "void __cdecl OutputResultOperations(char const *,char const *,bool,unsigned long,double)" (?OutputResultOperations@@YAXPBD0_NKN@Z)
The fix is to either (1) open cryptest.vcxproj
in notepad, find bench1.cpp
, and then rename it to bench.cpp
. Or (2) rename bench.cpp
to bench1.cpp
on the filesystem. Please don't delete this file.
解决方法是 (1)cryptest.vcxproj
在记事本中打开,找到bench1.cpp
,然后将其重命名为bench.cpp
. 或者 (2)在文件系统上重命名bench.cpp
为bench1.cpp
。请不要删除此文件。
The second problem is a little trickier because its a moving target. Down level releases, like 5.6.2 or 5.6.3, are missing the latest classes available in GitHub. The missing class files include HKDF (5.6.3), RDRAND (5.6.3), RDSEED (5.6.3), ChaCha (5.6.4), BLAKE2 (5.6.4), Poly1305 (5.6.4), etc.
第二个问题有点棘手,因为它是一个移动的目标。下级版本,如 5.6.2 或 5.6.3,缺少GitHub 中可用的最新类。缺失的类文件包括HKDF(5.6.3)、RDRAND(5.6.3)、RDSEED(5.6.3)、ChaCha(5.6.4)、BLAKE2(5.6.4)、Poly1305(5.6.4)等。
The fix is to remove the missing source files from the Visual Studio project files since they don't exist for the down level releases.
修复是从 Visual Studio 项目文件中删除丢失的源文件,因为它们不存在于下级版本。
Another option is to add the missing class files from the latest sources, but there could be complications. For example, many of the sources subtly depend upon the latest config.h
, cpu.h
and cpu.cpp
. The "subtlety" is you won't realize you are getting an under-performing class.
另一种选择是从最新来源添加缺少的类文件,但可能会出现复杂情况。例如,许多来源巧妙地依赖于最新的config.h
、cpu.h
和cpu.cpp
。“微妙之处”是你不会意识到你得到了一个表现不佳的班级。
An example of under-performing class is BLAKE2. config.h
adds compile time ARM-32 and ARM-64 detection. cpu.h
and cpu.cpp
adds runtime ARM instruction detection, which depends upon compile time detection. If you add BLAKE2 without the other files, then none of the detection occurs and you get a straight C/C++ implementation. You probably won't realize you are missing the NEON opportunity, which runs around 9 to 12 cycles-per-byte versus 40 cycles-per-byte or so for vanilla C/C++.
表现不佳的班级的一个例子是 BLAKE2。config.h
添加编译时 ARM-32 和 ARM-64 检测。cpu.h
并cpu.cpp
添加了运行时 ARM 指令检测,这取决于编译时检测。如果在没有其他文件的情况下添加 BLAKE2,则不会发生任何检测,并且您将获得直接的 C/C++ 实现。您可能不会意识到您错过了 NEON 机会,它每字节运行大约 9 到 12 个周期,而对于普通 C/C++ 则为每字节 40 个周期左右。
回答by Jan
I had this problem along with mismatch in ITERATOR_DEBUG_LEVEL. As a sunday-evening problem after all seemed ok and good to go, I was put out for some time. Working in de VS2017 IDE (Solution Explorer) I had recently added/copied a sourcefile reference to my project (ctrl-drag) from another project. Looking into properties->C/C++/Preprocessor - at source file level, not project level- I noticed that in a Release configuration _DEBUG was specified instead of NDEBUG for this source file. Which was all the change needed to get rid of the problem.
我遇到了这个问题以及 ITERATOR_DEBUG_LEVEL 中的不匹配。由于周日晚上的问题毕竟看起来不错而且很好,我被淘汰了一段时间。在 VS2017 IDE(解决方案资源管理器)中工作时,我最近从另一个项目中添加/复制了对我的项目(ctrl-drag)的源文件引用。查看属性-> C/C++/预处理器 -在源文件级别,而不是项目级别- 我注意到在发布配置中指定了 _DEBUG 而不是 NDEBUG 用于此源文件。这就是解决问题所需的全部更改。
回答by abhijithkp
Issue can be solved by adding CRT of msvcrtd.lib in the linker library. Because cryptlib.lib used CRT version of debug.
问题可以通过在链接器库中添加 msvcrtd.lib 的 CRT 来解决。因为 cryptlib.lib 使用了 CRT 版本的调试。