Skip to content

Instantly share code, notes, and snippets.

@atcarter714
Last active November 16, 2022 06:31
Show Gist options
  • Save atcarter714/6e3d93a86ae6d1f3c0606040e917ea27 to your computer and use it in GitHub Desktop.
Save atcarter714/6e3d93a86ae6d1f3c0606040e917ea27 to your computer and use it in GitHub Desktop.
Unity 2022.1 and 2022.2 beta fix for building your game: Without this code Unity cannot build your game and IL2CPP fails!
#if UNITY_EDITOR
using System;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
public class MsvcStdextWorkaround : IPreprocessBuildWithReport
{
const string kWorkaroundFlag = "/D_SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS";
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
var clEnv = Environment.GetEnvironmentVariable("_CL_");
if (string.IsNullOrEmpty(clEnv))
{
Environment.SetEnvironmentVariable("_CL_", kWorkaroundFlag);
}
else if (!clEnv.Contains(kWorkaroundFlag))
{
clEnv += " " + kWorkaroundFlag;
Environment.SetEnvironmentVariable("_CL_", clEnv);
}
}
}
#endif // UNITY_EDITOR
@atcarter714
Copy link
Author

Simply put this .cs code file anywhere in the project's "Assets" folder so that Unity picks it up and it will fix the problem with IL2CPP sputtering out a bunch of cryptic/useless error messages and failing to build (or even crashing the entire editor in some cases). This is the simplest fix to the issue which currently effects Unity 2022.2.0b14 (beta) and possibly other versions.

The fix is supposedly implemented in a handful of other (mostly older) Unity versions, but the current beta version (and potentially future ones) lack the fix ... it's apparently not documented or explained anywhere beyond some devs talking about or mentioning it on various forums and message boards and one post by a Unity engineer on the Unity forums that is very difficult to find. This issue has popped up for users on/off in different editor versions, and seems to be a recurring thing. So, here is your salvation when this frustrating and confusing problem crops up and tries to take a big bite out of your time and productivity!

(NOTE: If you are able to build your projects for Windows and UWP platforms without issue then you do not need this ... this only helps if the version of Unity you're using is incapable of building your game/app and gives you lots of confusing error messages that may or may not mention IL2CPP ...)

If you're building outside of the Unity editor for things like custom UWP deployment or HoloLens projects, then this C#-based fix may not work for you, and will require a different solution. In these situations, what you need to do is find the sparseconfig.h header file in one of your projects in your solution (very likely the one called "Il2CppOutputProject (PlatformName)" in the case of UWP solutions) and then add in the following preprocessor directive:

#define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS

That should be defined just above the existing SPARSEHASH_HASH and SPARSEHASH_HASH_NO_NAMESPACE preprocessor symbols in the file, within the active preprocessor block (in my case, and more than likely for you as well, that would be the first/primary big ol' fat #if block in the file and probably not the #else block which is commented as being for "POSIX" platforms). I uploaded a ready-made C/C++ header gist showing exactly how to do it which can be found here ... if, by any chance, someone is building their project on a system/compiler configuration where the second "POSIX" preprocessor block is the active one, then simply move it down there instead of where I put it, I guess, but I've never tested that. You might even try defining it at the very top of the file before the #if/#else blocks or add it to both blocks, the choice is yours, it just needs to be picked up by the preprocessor before IL2CPP tries to use any of the "sparsehash" stuff in code it generates, as it generates C++ code intended for older versions of the header files and it will fail utterly and completely (perhaps crash the editor like it did for me, lol) ...

Another approach would be to simply right-click your VC++ project in Solution Explorer and add _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS as a global preprocessor symbol/macro ... you could also define this as an environment variable, pass it to compilers that support preprocessor directives via command-line arguments, etc ... just make sure your compiler knows and has defined it so your project will build!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment