Getting errors in build, after I include torch in my cpp file

When i add # include <torch/torch.h> and then build project i get errors/warns like:

Unreal Projects\NoxViridus\eLibs\libtorch\include\c10\macros\Macros.h(495): error C4668: __GNUC__ is not defined as a preprocessor macro; for: #if/#elif is replaced by zero (0).
Unreal Projects\NoxViridus\eLibs\libtorch\include\c10\util\Lazy.h(32): error C2988: unrecognizable template declaration/definition
Unreal Projects\NoxViridus\eLibs\libtorch\include\c10\util\Lazy.h(32): note: the context for creating template instances (oldest first) is
Unreal Projects\NoxViridus\eLibs\libtorch\include\c10\util\Lazy.h(50): error C2334: Before: { are unexpected tokens; the part that looks like the function body is skipped.
\Unreal Projects\NoxViridus\eLibs\libtorch\include\c10\core\impl\DeviceGuardImplInterface.h(371): note: Consider using explicit overriding or comparing to nullptr if you want this warning not to appear.
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.38.33130\INCLUDE\atomic(1074): note: see declaration: std::_Atomic_storage<_Ty,8>::_TVal
1> with
1> [
1> _Ty=const c10::impl::DeviceGuardImplInterface *
1> ]

and it’s more of it like 15000 chars

the build then end with:

1>Trace file written to C:/Users/jmeno/AppData/Local/UnrealBuildTool/Log.uba with size 48.4kb
1>Total time in Unreal Build Accelerator local executor: 39.94 seconds
1>Total execution time: 47.39 seconds
1>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.MakeFile.Targets(44,5): error MSB3073: Command "C:\Program Files\Epic Games\UE_5.5\Engine\Build\BatchFiles\Build. bat" NoxViridus Win64 Development -Project="C:\Users\name\OneDrive\Documents\Unreal Projects\NoxViridus\NoxViridus.uproject" -WaitMutex -FromMsBuild -architecture=x64 was terminated with code 6.
1>The build of the NoxViridus.vcxproj project was completed: ERROR.
========== Build: successful: 0, failed: 1, current: 0, skipped: 0 ==========
========== Build completed at 18:24 and took 48.596 seconds ==========

When i remove the # include <torch/torch.h> it build succesfull and i downloaded libtorch stable(2.6.0),windows,libtorch,c++/java, cuda 12.6, RELEASE

my NoxViridus.Build.cs:

/*
// Fill out your copyright notice in the Description page of Project Settings.

using UnrealBuildTool;
//using System.IO;

public class NoxViridus : ModuleRules
{
    public NoxViridus(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        // Povolení RTTI a případně i výjimek (pokud je libtorch potřebuje)
        //bUseRTTI = true;
        //bEnableExceptions = true;

        // Public dependencies
        PublicDependencyModuleNames.AddRange(new string[]
        {
            "Core",
            "CoreUObject",
            "Engine",
            "InputCore",
            "OnlineSubsystem",
            "OnlineSubsystemUtils"
        });

        //PublicIncludePaths.Add(Path.Combine(ModuleDirectory, "/eLibs/libtorch/include"));
        //PublicIncludePaths.Add(Path.Combine(ModuleDirectory, "/Users/name/OneDrive/Dokumenty/Unreal Projects/NoxViridus/eLibs/libtorch/include"));
        //PublicIncludePaths.Add("C:/Users/name/OneDrive/Dokumenty/Unreal Projects/NoxViridus/eLibs/libtorch/include");

        // Přidáme také specifickou cestu k API hlavičkám, kde je např. torch.h.
        //PublicIncludePaths.Add("C:/Users/name/OneDrive/Dokumenty/Unreal Projects/NoxViridus/eLibs/libtorch/include/torch/csrc/api/include");

        // NastavenĂ­ knihovny
        //PublicAdditionalLibraries.Add("C:/Users/name/OneDrive/Dokumenty/Unreal Projects/NoxViridus/eLibs/libtorch/lib/torch.lib");

        //PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "/eLibs/libtorch/lib"));
        //PublicAdditionalLibraries.Add("torch.lib");

        // Uncomment if you are using Slate UI
        // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

        // Uncomment if you are using additional private modules
        // PrivateDependencyModuleNames.Add("OnlineSubsystem");

        // Add Steam support if needed in your uproject file
        // To include OnlineSubsystemSteam, enable it in the plugins section of your .uproject file
    }
}

*/
using UnrealBuildTool;
using System.IO;

public class NoxViridus : ModuleRules
{
    public NoxViridus(ReadOnlyTargetRules Target) : base(Target)
    {
        PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs;

        // Povolení RTTI a výjimek, pokud je libtorch potřebuje
        bUseRTTI = true;
        bEnableExceptions = true;
        CppStandard = CppStandardVersion.Cpp17;


        // Public dependencies
        PublicDependencyModuleNames.AddRange(new string[]
        {
            "Core",
            "CoreUObject",
            "Engine",
            "InputCore",
            "OnlineSubsystem",
            "OnlineSubsystemUtils"
        });

        // Cesta k libtorch knihovnám
        string LibTorchBaseDir = Path.Combine(ModuleDirectory, "../../eLibs/libtorch/");

        if (Target.Platform == UnrealTargetPlatform.Win64)
        {
            // Přidání include cest
            PublicIncludePaths.Add(Path.Combine(LibTorchBaseDir, "include"));
            PublicIncludePaths.Add(Path.Combine(LibTorchBaseDir, "include/torch/csrc/api/include"));

            // Přidání statických a dynamických knihoven
            string LibDir = Path.Combine(LibTorchBaseDir, "lib");
            string[] LibFiles = Directory.GetFiles(LibDir, "*.lib", SearchOption.AllDirectories);
            PublicAdditionalLibraries.AddRange(LibFiles);

            // Přidání runtime závislostí pro DLL
            string[] DllFiles = Directory.GetFiles(LibDir, "*.dll", SearchOption.AllDirectories);
            foreach (string DllPath in DllFiles)
            {
                string DllName = Path.GetFileName(DllPath);
                RuntimeDependencies.Add("$(TargetOutputDir)/" + DllName, DllPath);
                PublicDelayLoadDLLs.Add(DllName);
            }
        }
        PublicDefinitions.Add("TORCH_USE_CXX11_ABI=0");
    }
}

thanks for any replies

Look into

Make sure your plugin internals that conflict with Unreal’s defines are marked and isolated with

THIRD_PARTY_INCLUDES_START
and
THIRD_PARTY_INCLUDES_END

1 Like

Co if i understood IT corectly i need add in my project new plugin from thirt-party and inside of that plugin create .Build.cs file. And in to it write addresses to include lib folders ect. of libtorch?

It could be a plugin or a module but it is good practice to isolate third party code, so that it doesn’t have conflicting defines and macros.

1 Like

when i try to create third party library plugin template it fails to generate project files and deletes the plugin folder:

This tutorial pretty extensively explains incorporating thirdparty code into a project.

Note it’s from youtube from outside of the forum.

1 Like