Toch.dll not found when compiled on Windows

Hello,

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.
In detail:

the CmakeLists:

cmake_minimum_required (VERSION 3.8)
project (“example-app”)

set(CMAKE_CXX_STANDARD 17)

set(CMAKE_PREFIX_PATH C:/Users/Michael/Documents/GitHub/Torch_build/libtorch_win)
find_package(Torch REQUIRED)

set(TARGET_SOURCES example.cpp)
add_executable(example ${TARGET_SOURCES})

target_link_libraries(example “${TORCH_LIBRARIES}”)

The code in example.cpp:

#include <torch/torch.h>
#include

int main() {
torch::Tensor tensor = torch::rand({ 2, 3 });
std::cout << tensor << std::endl;
}

It compiles (granted with a zillion of warnings within the torch files) and produces an executable, but when I run it:

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?

best reagrds,
Michael

You’ll need to copy the DLLs to the same directory of the generated binary, because there is nothing like RPATH on Windows.

Hi @peterjc123,

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:

error

Environment:

  1. Visual Studio 2019
  2. CMake Version: 3.15.0-rc1
  3. Windows 10

Not sure what’s going wrong. Can you help with this?

Thanks!

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 [PythonRoot]\Lib\site-packages\torch\lib if you build using the conda/pip version or [LibTorchRoot]\lib if you use LibTorch.

Hi, Thanks for your response.

I had copied the libraries from [Libtorch_dir]\lib to the build\release destination. And I’m using the stable version.

Not sure what I’m missing out.

Just in case if this helps:

My cmake output from Visual Studio 2019 version.

1> CMake generation started for configuration: 'x64-Release'.
1> 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> Working directory: C:\Users\krshr\OneDrive\Documents\GitHub\Digit-3\out\build\x64-Release
1> [CMake] -- Looking for pthread.h
1> [CMake] -- Looking for pthread.h - not found
1> [CMake] -- Found Threads: TRUE  
1> [CMake] CMake Warning (dev) at C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Caffe2/public/utils.cmake:57 (if):
1> [CMake]   Policy CMP0054 is not set: Only interpret if() arguments as variables or
1> [CMake]   keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
1> [CMake]   details.  Use the cmake_policy command to set the policy and suppress this
1> [CMake]   warning.
1> [CMake] 
1> [CMake]   Quoted variables like "MSVC" will no longer be dereferenced when the policy
1> [CMake]   is set to NEW.  Since the policy is not set the OLD behavior will be used.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:121 (caffe2_interface_library)
1> [CMake]   C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:40 (find_package)
1> [CMake]   CMakeLists.txt:3 (find_package)
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] CMake Warning (dev) at C:/Users/krshr/Downloads/libtorch/libtorch/share/cmake/Torch/TorchConfig.cmake:80 (if):
1> [CMake]   Policy CMP0054 is not set: Only interpret if() arguments as variables or
1> [CMake]   keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
1> [CMake]   details.  Use the cmake_policy command to set the policy and suppress this
1> [CMake]   warning.
1> [CMake] 
1> [CMake]   Quoted variables like "MSVC" will no longer be dereferenced when the policy
1> [CMake]   is set to NEW.  Since the policy is not set the OLD behavior will be used.
1> [CMake] Call Stack (most recent call first):
1> [CMake]   CMakeLists.txt:3 (find_package)
1> [CMake] This warning is for project developers.  Use -Wno-dev to suppress it.
1> [CMake] -- Found torch: C:/Users/krshr/Downloads/libtorch/libtorch/lib/torch.lib  
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: C:/Users/krshr/OneDrive/Documents/GitHub/Digit-3/out/build/x64-Release
1> [CMake] 
1> Extracted includes paths.
1> Extracted CMake variables.
1> Extracted source files and headers.
1> Extracted code model.
1> CMake generation finished.

And this is the make output, it does seem to have many warnings but no errors.

https://pastebin.com/peS4LwKQ

Thanks for taking the time to answer, this has solved the problem indeed.

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?

Best regards,
Michael

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.

Maybe the binary is corrupted during download. Could you please try to download it again?

I tried downloading it again (Stable 1.1 Version). Still doesn’t work. :frowning_face:

I’m confused here. You have posted similar things on different threads with different results. So which one is the case for you?

My bad. Kindly ignore this post for now. I’ll try to follow CMake with Ninja first. Thanks for your help here.

PS: For others who come here and see this post, here is the other post I’m talking about: Unable to read Data using PyTorch C++ API (MNIST)