Help building libtorch example for Windows using CMake and Visual Studio 2017

Hi all -
I am attempting (unsuccessfully) to build the libtorch C++ example for Windows using cmake and Visual Studio 2017. I have downloaded the binaries, and have followed the instructions here:
https://pytorch.org/cppdocs/installing.html

Cmake ran until pthreads check, which is not included natively in the VS distribution and therefore it failed. How did everyone solve this?

Thanks,
Rob

Content of the CMakeError.log:

Determining if the include file pthread.h exists failed with the following output:
Change Dir: C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeTmp

Run Build Command:“C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/MSBuild/15.0/Bin/MSBuild.exe” “cmTC_338d1.vcxproj” “/p:Configuration=Debug” “/p:VisualStudioVersion=15.0”
Microsoft ® Build Engine version 15.9.21+g9802d43bc3 for .NET Framework

Copyright © Microsoft Corporation. All rights reserved.

Build started 1/23/2019 9:45:18 PM.

Project “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” on node 1 (default targets).

PrepareForBuild:

Creating directory “cmTC_338d1.dir\Debug”.

Creating directory “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\Debug”.

Creating directory “cmTC_338d1.dir\Debug\cmTC_338d1.tlog”.

InitializeBuildStatus:

Creating “cmTC_338d1.dir\Debug\cmTC_338d1.tlog\unsuccessfulbuild” because “AlwaysCreate” was specified.

ClCompile:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\HostX86\x64\CL.exe /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /D WIN32 /D _WINDOWS /D “CMAKE_INTDIR=“Debug”” /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_338d1.dir\Debug\" /Fd"cmTC_338d1.dir\Debug\vc141.pdb" /Gd /TC /FC /errorReport:queue C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\CheckIncludeFile.c

Microsoft ® C/C++ Optimizing Compiler Version 19.16.27026.1 for x64

Copyright © Microsoft Corporation. All rights reserved.

CheckIncludeFile.c

cl /c /Zi /W3 /WX- /diagnostics:classic /Od /Ob0 /D WIN32 /D _WINDOWS /D “CMAKE_INTDIR=“Debug”” /D _MBCS /Gm- /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"cmTC_338d1.dir\Debug\" /Fd"cmTC_338d1.dir\Debug\vc141.pdb" /Gd /TC /FC /errorReport:queue C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\CheckIncludeFile.c

c:\data\developer\deeplearning\pytorch\exampletorchapp\build\cmakefiles\cmaketmp\checkincludefile.c(1): fatal error C1083: Cannot open include file: ‘pthread.h’: No such file or directory [C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj]

Done Building Project “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” (default targets) – FAILED.

Build FAILED.

“C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” (default target) (1) ->

(ClCompile target) ->

c:\data\developer\deeplearning\pytorch\exampletorchapp\build\cmakefiles\cmaketmp\checkincludefile.c(1): fatal error C1083: Cannot open include file: ‘pthread.h’: No such file or directory [C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj]

0 Warning(s)

1 Error(s)

Time Elapsed 00:00:01.07

It may not be pthreads, but a CUDA error. Here is the console output:

C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build>cmake -D CMAKE_PREFIX_PATH=c:\DATA\Developer\DeepLearning\pytorch\libtorch_win\libtorch -G “Visual Studio 15 2017 Win64” …
– Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.17134.
– The C compiler identification is MSVC 19.16.27026.1
– The CXX compiler identification is MSVC 19.16.27026.1
– Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
– Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
– Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Detecting CXX compile features
– Detecting CXX compile features - done
– Looking for pthread.h
– Looking for pthread.h - not found
– Found Threads: TRUE
– Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0 (found suitable version “9.0”, minimum required is “7.0”)
– Caffe2: CUDA detected: 9.0
– Caffe2: CUDA nvcc is: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/bin/nvcc.exe
– Caffe2: CUDA toolkit directory: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0
– Caffe2: Header version is: 9.0
– Found CUDNN: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/include
– Found cuDNN: v7.4.1 (include: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/include, library: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/lib/x64/cudnn.lib)
– Autodetected CUDA architecture(s): 6.1
– Added CUDA NVCC flags for: -gencode;arch=compute_61,code=sm_61
CMake Error at C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Caffe2/public/cuda.cmake:368 (message):
** Unknown cmake build type:**
Call Stack (most recent call first):
C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)
C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Torch/TorchConfig.cmake:39 (find_package)
CMakeLists.txt:4 (find_package)

– Configuring incomplete, errors occurred!
See also “C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeOutput.log”.
See also “C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeError.log”.

C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build>

It’s not a CUDA problem, but rather a CMAKE_BUILD_TYPE problem…

try adding

-DCMAKE_BUILD_TYPE=Debug
or
-DCMAKE_BUILD_TYPE=Release

My case was

Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN
libraries. Please set the proper cuDNN prefixes and / or install cuDNN.

CuDNN was uninstalled by mistake days before.

In this tutorial, I explain step by step how to setup a project in Visual Studio 2019 and run a sample code. Hope this helps!

https://towardsdatascience.com/setting-up-a-c-project-in-visual-studio-2019-with-libtorch-1-6-ad8a0e49e82c