Hello,
I’m using Visual Studio 2019 and I’m trying to build a DLL that includes PyTorch c++ CPU only. I’d like to do this because the DLL will later be used in two different applications one in c++ and the other in C#.
When I’m compiling I mainly get this error:
Macro clash with min and max -- define NOMINMAX when compiling your program on Windows
out of +1700 errors.
What I’m doing is the following:

I created a DLL project from the List that Visual Studio offers.
I set up Visual Studio preferences as shown in the following link: https://expoundai.wordpress.com/2020/10/13/setting-up-a-cpp-project-in-visual-studio-2019-with-libtorch-1-6/. I trust these preferences, because I made a .exe application and it worked flawlessly.
I made a super easy code, that didn’t use PyTorch to make sure I’m able to run it from C# and c++.

#include "pch.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

extern "C"
{
    __declspec(dllexport) int sum(int a, int b)
    {
        return (a + b);
    }
}


Code in point 3 works perfectly, so now its time to include the torch libraries and test the code again.

#include "pch.h"

#include <torch/torch.h>   // <---------- Only modification in previous code
#include <torch/script.h>  // <---------- Only modification in previous code

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

extern "C"
{
    __declspec(dllexport) int sum(int a, int b)
    {
        return (a + b);
    }
}

When I try to compile this code, all the errors appear (attached image).

pytorchcrashes1885×361 77 KB

I’ve tried many things, including adding NOMINMAX to preprocessor definitions. The DLL compiles, but calling it from c# or c++ makes the application crash.
Also tried to:
#undef max
#undef min

in C++17.h, it compiles but happens the same as before.
I’ve also searched the forums and could’nt find anything similar.
Any clue how to make this work?
Thanks in advance,
Pau