Toch.dll not found when compiled on Windows

username: Mikele
cooked: <p>Hello,</p>
<p>I am once again trying to use torch on windows 10, but it keeps giving me headaches. I have now tried to use the VC compiler (14.20) to compile the tutorial project, but the linking part seems to fail.<br>
In detail:</p>
<p>the CmakeLists:</p>
<blockquote>
<p>cmake_minimum_required (VERSION 3.8)<br>
project (“example-app”)</p>
<p>set(CMAKE_CXX_STANDARD 17)</p>
<p>set(CMAKE_PREFIX_PATH C:/Users/Michael/Documents/GitHub/Torch_build/libtorch_win)<br>
find_package(Torch REQUIRED)</p>
<p>set(TARGET_SOURCES example.cpp)<br>
add_executable(example ${TARGET_SOURCES})</p>
<p>target_link_libraries(example “${TORCH_LIBRARIES}”)</p>
</blockquote>
<p>The code in example.cpp:</p>
<blockquote>
<p><span class="hashtag-raw">#include</span> &lt;torch/torch.h&gt;<br>
<span class="hashtag-raw">#include</span> </p>
<p>int main() {<br>
torch::Tensor tensor = torch::rand({ 2, 3 });<br>
std::cout &lt;&lt; tensor &lt;&lt; std::endl;<br>
}</p>
</blockquote>
<p>It compiles (granted with a zillion of warnings within the torch files) and produces an executable, but when I run it:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/79f26124446c45073571be6d3b25d29ca08f8fad.png" data-download-href="https://discuss.pytorch.org/uploads/default/79f26124446c45073571be6d3b25d29ca08f8fad" title="dllerror"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/7/79f26124446c45073571be6d3b25d29ca08f8fad_2_690x275.png" alt="dllerror" data-base62-sha1="hoN4jeXo0RVZyN6SdEHvmv7bMzX" width="690" height="275" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/7/79f26124446c45073571be6d3b25d29ca08f8fad_2_690x275.png, https://discuss.pytorch.org/uploads/default/optimized/2X/7/79f26124446c45073571be6d3b25d29ca08f8fad_2_1035x412.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/2X/7/79f26124446c45073571be6d3b25d29ca08f8fad_2_1380x550.png 2x" data-dominant-color="67686A"><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">dllerror</span><span class="informations">2659×1063 99 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 dont get these errors on Ubuntu and can work with torch fine on it. It should find and link the torch.dll automatically given the linking command in Cmake, shouldn’t it? Can someone point me towards the reason why this is failing for me on Windows?</p>
<p>best reagrds,<br>
Michael</p>

username: peterjc123
cooked: <p>You’ll need to copy the DLLs to the same directory of the generated binary, because there is nothing like <code>RPATH</code> on Windows.</p>

username: krshrimali
cooked: <p>Hi <a class="mention" href="/u/peterjc123">@peterjc123</a>,</p>
<p>I was facing the same problem as mentioned. I copied the DLLs in the build directory (where the executable file is located), but it gives the following error:</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/2X/1/183ad1b8a631322b578a10c5ca67c9be849a2527.png" alt="error" data-base62-sha1="3sltGSrOiY5fhSms2mKQWrDGAGH" width="600" height="268"></p>
<p>Environment:</p>
<ol>
<li>Visual Studio 2019</li>
<li>CMake Version: 3.15.0-rc1</li>
<li>Windows 10</li>
</ol>
<p>Not sure what’s going wrong. Can you help with this?</p>
<p>Thanks!</p>

username: peterjc123
cooked: <p>You are using the wrong libraries I guess. If you built PyTorch yourself, then you should use your own copy. Otherwise, please copy those from <code>[PythonRoot]\Lib\site-packages\torch\lib</code> if you build using the conda/pip version or <code>[LibTorchRoot]\lib</code> if you use LibTorch.</p>

username: krshrimali
cooked: <p>Hi, Thanks for your response.</p>
<p>I had copied the libraries from [Libtorch_dir]\lib to the build\release destination. And I’m using the stable version.</p>
<p>Not sure what I’m missing out.</p>

username: krshrimali
cooked: <p>Just in case if this helps:</p>
<p>My cmake output from Visual Studio 2019 version.</p>
<pre><code class="lang-auto">1&gt; CMake generation started for configuration: 'x64-Release'.
1&gt; Command line: "C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe"  -G "Ninja" -DCMAKE_INSTALL_PREFIX:PATH="C:\Users\krshr\OneDrive\Documents\GitHub\Digit-3\out\install\x64-Release" -DCMAKE_CXX_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/HostX64/x64/cl.exe" -DCMAKE_C_COMPILER:FILEPATH="C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.21.27702/bin/HostX64/x64/cl.exe" -DCMAKE_PREFIX_PATH=C:\Users\krshr\Downloads\libtorch\libtorch -DCMAKE_BUILD_TYPE="Release" -DCMAKE_MAKE_PROGRAM="C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO\2019\COMMUNITY\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "C:\Users\krshr\OneDrive\Documents\GitHub\Digit-3"
1&gt; Working directory: C:\Users\krshr\OneDrive\Documents\GitHub\Digit-3\out\build\x64-Release
1&gt; [CMake] -- Looking for pthread.h
1&gt; [CMake] -- Looking for pthread.h - not found
1&gt; [CMake] -- Found Threads: TRUE  
1&gt; [CMake] CMake Warning (dev) at C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Caffe2/public/utils.cmake:57 (if):
1&gt; [CMake]   Policy CMP0054 is not set: Only interpret if() arguments as variables or
1&gt; [CMake]   keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
1&gt; [CMake]   details.  Use the cmake_policy command to set the policy and suppress this
1&gt; [CMake]   warning.
1&gt; [CMake] 
1&gt; [CMake]   Quoted variables like "MSVC" will no longer be dereferenced when the policy
1&gt; [CMake]   is set to NEW.  Since the policy is not set the OLD behavior will be used.
1&gt; [CMake] Call Stack (most recent call first):
1&gt; [CMake]   C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:121 (caffe2_interface_library)
1&gt; [CMake]   C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:40 (find_package)
1&gt; [CMake]   CMakeLists.txt:3 (find_package)
1&gt; [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1&gt; [CMake] CMake Warning (dev) at C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:80 (if):
1&gt; [CMake]   Policy CMP0054 is not set: Only interpret if() arguments as variables or
1&gt; [CMake]   keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
1&gt; [CMake]   details.  Use the cmake_policy command to set the policy and suppress this
1&gt; [CMake]   warning.
1&gt; [CMake] 
1&gt; [CMake]   Quoted variables like "MSVC" will no longer be dereferenced when the policy
1&gt; [CMake]   is set to NEW.  Since the policy is not set the OLD behavior will be used.
1&gt; [CMake] Call Stack (most recent call first):
1&gt; [CMake]   CMakeLists.txt:3 (find_package)
1&gt; [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1&gt; [CMake] -- Found torch: C:/Users/krshr/Downloads/libtorch/libtorch/lib/torch.lib  
1&gt; [CMake] -- Configuring done
1&gt; [CMake] -- Generating done
1&gt; [CMake] -- Build files have been written to: C:/Users/krshr/OneDrive/Documents/GitHub/Digit-3/out/build/x64-Release
1&gt; [CMake] 
1&gt; Extracted includes paths.
1&gt; Extracted CMake variables.
1&gt; Extracted source files and headers.
1&gt; Extracted code model.
1&gt; CMake generation finished.
</code></pre>

username: krshrimali
cooked: <p>And this is the make output, it does seem to have many warnings but no errors.</p>
<p><a href="https://pastebin.com/peS4LwKQ" class="onebox" target="_blank" rel="nofollow noopener">https://pastebin.com/peS4LwKQ</a></p>

username: Mikele
cooked: <p>Thanks for taking the time to answer, this has solved the problem indeed.</p>
<p>Is there a smarter way of installing the torch libraries somewhere central in windows, so that one doesn’t have to copy them for each separate build?</p>
<p>Best regards,<br>
Michael</p>

username: peterjc123
cooked: <p>If you use CMake with Ninja, I believe it should be fine. However, I have no idea how could that be improved if you create a VS project manually.</p>

username: peterjc123
cooked: <p>Maybe the binary is corrupted during download. Could you please try to download it again?</p>

username: krshrimali
cooked: <p>I tried downloading it again (Stable 1.1 Version). Still doesn’t work. <img src="https://discuss.pytorch.org/images/emoji/apple/frowning_face.png?v=9" title=":frowning_face:" class="emoji" alt=":frowning_face:"></p>

username: peterjc123
cooked: <p>I’m confused here. You have posted similar things on different threads with different results. So which one is the case for you?</p>

username: krshrimali
cooked: <p>My bad. Kindly ignore this post for now. I’ll try to follow CMake with Ninja first. Thanks for your help here.</p>
<p>PS: For others who come here and see this post, here is the other post I’m talking about: <a href="https://discuss.pytorch.org/t/unable-to-read-data-using-pytorch-c-api-mnist/47915/13" class="inline-onebox">Unable to read Data using PyTorch C++ API (MNIST)</a></p>