Trying to link CryptPP to UE4 project but failing to compile

Hello all. I’d like to use HMAC-SHA1 from CryptPP. I updated my Build.cs to add the module and then used CryptPP headers. After trying to use the library and compiling, I get “mismatch detected” errors.

My current Build.cs:

public class {projectname} : ModuleRules
{
	private string ModulePath
	{
		get { return ModuleDirectory; }
	}
 
		private string ThirdPartyPath
	{
		get { return Path.GetFullPath( Path.Combine( ModulePath, "../../ThirdParty/" ) ); }
	}
		
	public {projectname}(TargetInfo Target)
	{
		PublicDependencyModuleNames.AddRange(new string[] { 
		"Core", 
		"CoreUObject", 
		"Engine", 
		"HTTP",
		"InputCore", 
		"HeadMountedDisplay",
		"Json", "JsonUtilities",
		"CryptoPP"
		});
		
		if ((Target.Platform == UnrealTargetPlatform.Win64) || (Target.Platform == UnrealTargetPlatform.Win32))
		 {
			 AddEngineThirdPartyPrivateStaticDependencies(Target, "CryptoPP");
		 }
		
		PublicIncludePathModuleNames.Add("CryptoPP");
		
		PublicIncludePaths.Add(Path.Combine(ThirdPartyPath, "CryptoPP", "5.6.2/include"));
 
		PublicIncludePaths.Add("CryptoPP/5.6.2/include");
	}
}

CryptPP headers added:

#include "cryptlib.h"
#include "hmac.h"
#include "sha.h"

.cpp using the library:

CryptoPP::SecByteBlock key;
CryptoPP::HMAC<CryptoPP::SHA512> MyInstance(key, key.size());

Compile error I’m getting:

1>cryptlib.lib(cryptlib.obj) : error LNK2038: mismatch detected for '_MSC_VER': value '1800' doesn't match value '1900' in PCH.ue4twitter.h.obj

Researching the error it looks like I’m using different compiler versions for the static library and my project. I even tried recompiling the library but no success.

Not sure if it means something, but if I don’t use the library but keep only the includes from CryptPP it actually compiles.

Using Visual Studio 2015.

Full compiler output

Apparently some versions of the engine comes with the library included. I didn’t come to any conclusion, but it looks like only source builds actually comes with it. It doesn’t matter, because it didn’t work for me.
The solution is simpler than I thought, just follow these steps:

  • Download CryptoPP 5.6.2 from source. (or the currently used version from the Build.cs)
  • Unzip, and open the .sln (solution) file.
  • It will probably ask you to upgrade, just accept it.
  • When done, right-click cryptlib and go to Properties.
  • Set Configuration as Release and Platform as x64.
  • Go to Configuration Properties > C/C++ > Code Generation and set Runtime Library as Multi-threaded DLL (/MD), this is how UE4 expects a library to be compiled (that’s as far as I was able to understand it). Press OK.
  • Right-click cryptlib and Build.
  • When you’re done, there will be a new .lib file created in \x64\Output\Release\cryptlib.lib. Copy that to \Engine\Source\ThirdParty\CryptoPP\5.6.2\lib\Win64\VS2013.
  • For the include files, I copied the whole library. You could probably copy just the .h files. So copy the files to \Engine\Source\ThirdParty\CryptoPP\5.6.2\include.

A thing to notice is that this is standard setup for any static library. There’s also a guide for how to do it for any static library. We can skip the Build.cs part because CryptoPP is already in ue4 and contains its own Build.cs, so we just add the module.