Compilation error in DLL

username: Pau_DC
cooked: <p>Hello,</p>
<p>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#.<br>
When I’m compiling I mainly get this error:<br>
<code>Macro clash with min and max -- define NOMINMAX when compiling your program on Windows</code><br>
out of +1700 errors.</p>
<p>What I’m doing is the following:</p>
<ol>
<li>I created a DLL project from the List that Visual Studio offers.</li>
<li>I set up Visual Studio preferences as shown in the following link: <a href="https://expoundai.wordpress.com/2020/10/13/setting-up-a-cpp-project-in-visual-studio-2019-with-libtorch-1-6/" rel="noopener nofollow ugc">https://expoundai.wordpress.com/2020/10/13/setting-up-a-cpp-project-in-visual-studio-2019-with-libtorch-1-6/</a>. I trust these preferences, because I made a .exe application and it worked flawlessly.</li>
<li>I made a super easy code, that didn’t use PyTorch to make sure I’m able to run it from C# and c++.</li>
</ol>
<pre><code class="lang-auto">#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></pre>
<ol start="4">
<li>Code in point 3 works perfectly, so now its time to include the torch libraries and test the code again.</li>
</ol>
<pre><code class="lang-auto">#include "pch.h"

#include &lt;torch/torch.h&gt;   // &lt;---------- Only modification in previous code
#include &lt;torch/script.h&gt;  // &lt;---------- 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);
    }
}
</code></pre>
<p>When I try to compile this code, all the errors appear (attached image).<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/2/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a" title="pytorchcrashes"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a_2_690x132.jpeg" alt="pytorchcrashes" data-base62-sha1="u09gOdLklxKvlaDCIkBmrYazeTo" width="690" height="132" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a_2_690x132.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a_2_1035x198.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/d/2/d2458bdc99f99384edb4ddfe7d22fb9e6c09796a_2_1380x264.jpeg 2x" data-dominant-color="292E34"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorchcrashes</span><span class="informations">1885×361 77 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I’ve tried many things, including adding <code>NOMINMAX</code> to preprocessor definitions. The DLL compiles, but calling it from c# or c++ makes the application crash.<br>
Also tried to:</p>
<pre><code class="lang-auto">#undef max
#undef min
</code></pre>
<p>in C++17.h, it compiles but happens the same as before.<br>
I’ve also searched the forums and could’nt find anything similar.</p>
<p>Any clue how to make this work?</p>
<p>Thanks in advance,<br>
Pau</p>

username: Matej_Kompanek
cooked: <p>I use the <code>NOMINMAX</code> 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# ?</p>

username: Pau_DC
cooked: <p>Hello, thanks for replying.<br>
I’m not sure the crash is related to the preprocessor definitions. I’m not sure where it’s coming from.</p>
<p>In C# I get a DllNotFoundException. I make the same process that I did in the step 2 that worked fine.<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fc7deb787d3e95005898c4a1cd4f80dbad7757e2.jpeg" alt="pytorchcrashes_1" data-base62-sha1="A1E6tGkf3riDEBiEhakQ2nKpCPU" width="437" height="152"></p>
<p>I call it with the following code:</p>
<pre><code class="lang-auto">public const string CppFunctionsDLL = @"DycareAPIBLE\Dependencies\Dll1.dll";
[DllImport(CppFunctionsDLL, CallingConvention = CallingConvention.Cdecl)]
extern static int sum(int a, int b);
</code></pre>
<p><code>var a = sum(2, 7);</code></p>
<p>I’ve also used a dependency walker. This one: <a href="https://github.com/lucasg/Dependencies" rel="noopener nofollow ugc">Dependencies</a> and nothing weird shows up.</p>
<p>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?</p>

username: Matej_Kompanek
cooked: <p>Look here:</p><aside class="quote" data-post="1" data-topic="133998">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/haryngod/48/42917_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/c-c-is-it-possible-to-use-c-libtorch-dll-in-c/133998">[c++][c#] Is it possible to use c++(libtorch) dll in c#</a> <a class="badge-category__wrapper " href="/c/c/11"><span data-category-id="11" data-drop-close="true" class="badge-category " title="Topics related to the C++ Frontend, C++ API or C++ Extensions"><span class="badge-category__name">C++</span></span></a>
  </div>
  <blockquote>
    I tried to use libtorch in c#. 
I made P/Invoke dll which is wrapped libtorch. 
But when I use it in c# application, it cannot load my custom dll. 
I wonder is there anyone make c++ dll with libtorch and run it in c# application?
  </blockquote>
</aside>

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

username: Pau_DC
cooked: <p>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.</p>
<p>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?<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/7/37f8654d097ef0c6565370696fde26be481905ae.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/37f8654d097ef0c6565370696fde26be481905ae" title="pytorchcrashes_2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37f8654d097ef0c6565370696fde26be481905ae_2_690x377.jpeg" alt="pytorchcrashes_2" data-base62-sha1="7Z8rFhbVRNVpc85z7J4uMLnSVc2" width="690" height="377" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37f8654d097ef0c6565370696fde26be481905ae_2_690x377.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37f8654d097ef0c6565370696fde26be481905ae_2_1035x565.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/7/37f8654d097ef0c6565370696fde26be481905ae_2_1380x754.jpeg 2x" data-dominant-color="F4F3F3"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">pytorchcrashes_2</span><span class="informations">1920×1050 249 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>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<br>
But the error is still there.</p>
<p>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:</p>
<pre><code class="lang-auto">Build Action -&gt; Content
Copy to Output Directory -&gt; Copy always
</code></pre>

username: Matej_Kompanek
cooked: <p>In your C# code I saw follwing …</p>
<pre><code class="lang-auto">public const string CppFunctionsDLL = @"DycareAPIBLE\Dependencies\Dll1.dll";
</code></pre>
<p>Does it mean your DLL is in ‘DycareAPIBLE\Dependencies’ ?<br>
You could try using just  [DllImport(“Dll1.dll”)] and place the dll in the root folder along the pytorch DLLs</p>

username: Pau_DC
cooked: <p>Hey, thanks for reaching out and helping me out.</p>
<p>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.</p>
<p>Now it’s time to find what’s making it crash in the other solution.</p>