4

UnrealEngine 4.19 uses Visual Studio 2017's C++14 mode as default. The Visual Studio 2017 compiler has a flag /std:c++17 to enable C++17 and subsequently things like <optional> for std::optional.

I tried adding the following line to VCToolChain.cs (C:\Program Files\Epic Games\UE_4.19\Engine\Source\Programs\UnrealBuildTool\Platform\Windows\VCToolChain.cs):

void AppendCLArguments_Global(CppCompileEnvironment CompileEnvironment, VCEnvironment EnvVars, List<string> Arguments)
    {
        Arguments.Add("/std:c++17");
        // ... the rest of this function.

However this doesn't seem to do anything, for example #include <optional> still results in the build error fatal error C1189: #error: class template optional is only available with C++17.. It's immensely difficult to find out anything about this or even try to find out with that compiler flags the UnrealBuildTool (UBT) is calling cl.exe... Other answers suggested to add -verbose to the nmake "Build Command Line" setting (so mine looks like: "C:\Program Files\Epic Games\UE_4.19\Engine\Build\BatchFiles\Build.bat" DemoApp Win64 DebugGame "$(SolutionDir)$(ProjectName).uproject" -WaitMutex -FromMsBuild -verbose but all it does is output verbose UBT messages, not compiler command-line invocation output.

Ela782
  • 4,543
  • 5
  • 46
  • 61
  • 1
    Would it work to add it to the project file through the Language tab? – Carl Jun 16 '18 at 11:36
  • @Carl if you refer to the "normal" VS project settings under "Properties": There is no such thing in Unreal Engine projects. As mentioned, UE has its own build system, so there isn't any Language tab or settings to change there. At all. – Ela782 Jun 16 '18 at 22:35

1 Answers1

2

Short answer : Add -CppStd=Cpp17 to your UnrealBuildTool.exe compile command.

Strangely, this seems to be documented nowhere, I had to dig into the .cs files of the engine to find it.

More specifically, in the file "C:/Program Files (x86)/Epic Games/UE_4.26/Engine/Source/Programs/UnrealBuildTool/Configuration/TargetRules.cs", there is :

        /// <summary>
        /// Which C++ stanard to use for compiling this target
        /// </summary>
        [RequiresUniqueBuildEnvironment]
        [CommandLine("-CppStd")]
        [XmlConfigFile(Category = "BuildConfiguration")]
        public CppStandardVersion CppStandard = CppStandardVersion.Default;

Then, in another file, the definition of CppStandardVersion is :

 public enum CppStandardVersion
    {
        /// <summary>
        /// Use the default standard version
        /// </summary>
        Default,

        /// <summary>
        /// Supports C++14
        /// </summary>
        Cpp14,

        /// <summary>
        /// Supports C++17
        /// </summary>
        Cpp17,

        /// <summary>
        /// Latest standard supported by the compiler
        /// </summary>
        Latest,
    }

So, I have generated my project files with -cmakefile, then I have opened my CMakeLists.txt and added -CppStd=Latest to my Build.bat compile command, and the error with std::optional is gone !

god
  • 1,578
  • 1
  • 9
  • 7