Compilation error in DLL

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:

  1. I created a DLL project from the List that Visual Studio offers.
  2. 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.
  3. 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);
    }
}
  1. 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).

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

I use the NOMINMAX macro and call my dll from C#, everything works fine. If it compiles, are you sure the crash is related to preprocessor definitions ? Is there error message when you try to call it from C# ?

1 Like

Hello, thanks for replying.
I’m not sure the crash is related to the preprocessor definitions. I’m not sure where it’s coming from.

In C# I get a DllNotFoundException. I make the same process that I did in the step 2 that worked fine.
pytorchcrashes_1

I call it with the following code:

public const string CppFunctionsDLL = @"DycareAPIBLE\Dependencies\Dll1.dll";
[DllImport(CppFunctionsDLL, CallingConvention = CallingConvention.Cdecl)]
extern static int sum(int a, int b);

var a = sum(2, 7);

I’ve also used a dependency walker. This one: Dependencies and nothing weird shows up.

From your experience, if the code in step 2 works and I add NOMINMAX to preprocessor and add the includes to the code. This should work fine?

Look here:

Is your C# assembly set to x64 and are all pytorch DLLs in the same directory ?
I don’t think ‘DLL not found’ is related to build.

1 Like

Oh! I saw that topic the other day. That’s the reason I used a Dependency Walker. Firstly I used the one you mentioned there and then the other I linked in my previous reply.

I don’t see any red folder, what I see are a few yellow question marks. And there are some errors I don’t know how to solve in the middle box. Is this normal?

I’ve also followed your recommendation there and manually copied ALL the files from libtorch-win-shared-with-deps-1.10.0+cpu\libtorch\lib to my output folder. In my case there are 13 files, since I’m not using the CUDA version
But the error is still there.

Both my DLL and C# application are compiled in Debug x64. What I did is to create a folder in my solution, in which I copied my DLL and all necessary DLLs, I set every file to:

Build Action -> Content
Copy to Output Directory -> Copy always

In your C# code I saw follwing …

public const string CppFunctionsDLL = @"DycareAPIBLE\Dependencies\Dll1.dll";

Does it mean your DLL is in ‘DycareAPIBLE\Dependencies’ ?
You could try using just [DllImport(“Dll1.dll”)] and place the dll in the root folder along the pytorch DLLs

1 Like

Hey, thanks for reaching out and helping me out.

I think I’ve a configuration problem in my C# application, I made a new solution and I was able to run it without issues.

Now it’s time to find what’s making it crash in the other solution.