Can't build libtorch following offical tutorial

I am attempting to follow this tutorial: Installing C++ Distributions of PyTorch — PyTorch main documentation

I have followed it exactly besides changing the name of my project, and of course filling out the actual location of libtorch in place of the dummy location.

However, the prescribed steps fail.

$ cmake -DCMAKE_PREFIX_PATH=/c/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch ..
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.19045.
-- The C compiler identification is MSVC 19.29.30148.0
-- The CXX compiler identification is MSVC 19.29.30148.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/BuildTools/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.6 (found version "11.6") 
CMake Warning (dev) at C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Caffe2/public/cuda.cmake:44 (if):
  Policy CMP0054 is not set: Only interpret if() arguments as variables or
  keywords when unquoted.  Run "cmake --help-policy CMP0054" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  Quoted variables like "MSVC" will no longer be dereferenced when the policy
  is set to NEW.  Since the policy is not set the OLD behavior will be used.
Call Stack (most recent call first):
  C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
  C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:4 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.

CMake Error at C:/Program Files/CMake/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:501 (message):
  No CUDA toolset found.
Call Stack (most recent call first):
  C:/Program Files/CMake/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:8 (CMAKE_DETERMINE_COMPILER_ID_BUILD)
  C:/Program Files/CMake/share/cmake-3.26/Modules/CMakeDetermineCompilerId.cmake:53 (__determine_compiler_id_test)
  C:/Program Files/CMake/share/cmake-3.26/Modules/CMakeDetermineCUDACompiler.cmake:307 (CMAKE_DETERMINE_COMPILER_ID)
  C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Caffe2/public/cuda.cmake:47 (enable_language)
  C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
  C:/Users/wesle/source/libtorch-win-shared-with-deps-2.0.1+cu118/libtorch/share/cmake/Torch/TorchConfig.cmake:68 (find_package)
  CMakeLists.txt:4 (find_package)


-- Configuring incomplete, errors occurred!

Note that just before the warnings about using quotations, I am notified that CUDA was found.

What I tried

Searching around I tried a couple of things that did not work:

  • Set the CMAKE_CUDA_COMPILER env variable to the location of nvcc. I tried it with and without .exe on the end. Neither changed the outcome.

  • set the same variable in CMakeLists.txt

  • Finally, the only other thing I find odd is that one line of the output you see mention of two different versions of MSVC:

-- The CXX compiler identification is MSVC 19.29.30148.0

And on another, you see

-- Check for working CXX compiler: C:/.../MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped

I know nothing about cmake or compiler commands, so have no idea how to troubleshoot this. Any help is appreciated.

I found this thread solved my problem:

There are 4 files located at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.7\extras\visual_studio_integration\MSBuildExtensions

Copy and paste those files to either:

  • If you are just using build tools, but don’t have the Visual Studio IDE installed: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VC\v160\BuildCustomizations
  • If you are using Visual Studio IDE: C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Microsoft\VC\v160\BuildCustomizations

Note that the year and edition of the IDE might be different for you.

This requirement should probably be added to the tutorial. But I sometimes think the devs and tut writers don’t care about providing good directions for those of us using Windows. I could be wrong but I think we are an afterthought.