Mismatch Detected for 'RuntimeLibrary' Ask Question

Mismatch Detected for 'RuntimeLibrary' Ask Question

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

以下も得られます:

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

私がコンパイルしようとしたコードは単純なものでした (これは別のサイトから取得したものです):

#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;
}

これを修正する方法はありますか? 現時点で必要なのは SHA-256 だけで、他には何もありません。Windows 7 64 ビットを使用しており、VS C++ を今日ダウンロードしたので、最新バージョンのはずです。

ベストアンサー1

(これはすでにコメントで回答されていますが、実際の答え、私はこれを書いています。

この問題は、Visual C++の新しいバージョンで発生します(古いバージョンでは通常、プログラムを黙ってリンクするだけで、実行時にクラッシュします)。これは、プログラムにリンクしているライブラリの一部(またはプログラム自体のソースファイルの一部)がCRT (C ランタイム ライブラリ) の異なるバージョンを使用します。

Project Propertiesこのエラーを修正するには、 (および/または使用しているライブラリの )に移動しC/C++、 、 の順にCode Generation移動して の値を確認する必要がありRuntime Libraryます。これは、 とまったく同じになるはずです。全てリンクするファイルとライブラリ。(DLL とのリンクではルールが少し緩和されますが、ここでは「理由」や詳細については説明しません。)

現在、この設定には 4 つのオプションがあります。

  1. マルチスレッドデバッグ
  2. マルチスレッド デバッグ DLL
  3. マルチスレッドリリース
  4. マルチスレッドリリースDLL

あなたの特定の問題は、「マルチスレッドデバッグ」を使用して構築されたライブラリ(つまり、静的マルチスレッドデバッグCRT)を、「マルチスレッドデバッグ」を使用して構築されているプログラムにリンクすることから生じているようです。DLL「設定(つまり、動的マルチスレッド デバッグ CRT)です。この設定は、ライブラリまたはプログラムで変更する必要があります。今のところ、プログラムでこれを変更することをお勧めします。

Visual Studio プロジェクトでは、デバッグ ビルドとリリース ビルド (および 32/64 ビット ビルド) に異なるプロジェクト設定セットが使用されるため、これらすべてのプロジェクト構成で設定が一致していることを確認する必要があります。

さらに詳しい情報については、以下を参照してください(上記のコメントからリンクされています)。

  1. リンカー ツールの警告 LNK4098MSDNで
  2. /MD、/ML、/MT、/LD (ランタイム ライブラリの使用)MSDNで
  3. VC11 ベータ版のビルド エラー - MTd ライブラリと MDd 実行ファイルが混在するとリンクに失敗するBugzilla@Mozilla で

アップデート: (なぜそこまで注意が必要なのかというコメントへの返答です。)

リンクしている2つのコード自体が標準ライブラリにリンクしていて、標準ライブラリを使用している場合、標準ライブラリは両方のコードで同じである必要があります。ただし、素晴らしい2 つのコード部分がどのように相互作用し、データを渡すかについては注意が必要です。一般的に、ほとんどすべての状況で、標準ライブラリ ランタイムのまったく同じバージョンを使用するだけで十分です (デバッグ/リリース、スレッド、そしてもちろん Visual C++ のバージョン、反復子デバッグなど)。

この問題の最も重要な部分は次のとおりです。関数呼び出しの両側にあるオブジェクトのサイズについて同じ考えを持つこと

Consider for example that the above two pieces of code are called A and B. A is compiled against 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.)

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

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

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!

おすすめ記事