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

rtrahms: <p>Hi all -<br>
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:<br>
<a href="https://pytorch.org/cppdocs/installing.html" class="onebox" target="_blank" rel="nofollow noopener">https://pytorch.org/cppdocs/installing.html</a></p>
<p>Cmake ran until pthreads check, which is not included natively in the VS distribution and therefore it failed.  How did everyone solve this?</p>
<p>Thanks,<br>
Rob</p>

rtrahms: <p>Content of the CMakeError.log:</p>
<p>Determining if the include file pthread.h exists failed with the following output:<br>
Change Dir: C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeTmp</p>
<p>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”<br>
Microsoft ® Build Engine version 15.9.21+g9802d43bc3 for .NET Framework</p>
<p>Copyright © Microsoft Corporation. All rights reserved.</p>
<p>Build started 1/23/2019 9:45:18 PM.</p>
<p>Project “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” on node 1 (default targets).</p>
<p>PrepareForBuild:</p>
<p>Creating directory “cmTC_338d1.dir\Debug”.</p>
<p>Creating directory “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\Debug”.</p>
<p>Creating directory “cmTC_338d1.dir\Debug\cmTC_338d1.tlog”.</p>
<p>InitializeBuildStatus:</p>
<p>Creating “cmTC_338d1.dir\Debug\cmTC_338d1.tlog\unsuccessfulbuild” because “AlwaysCreate” was specified.</p>
<p>ClCompile:</p>
<p>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</p>
<p>Microsoft ® C/C++ Optimizing Compiler Version 19.16.27026.1 for x64</p>
<p>Copyright © Microsoft Corporation.  All rights reserved.</p>
<p>CheckIncludeFile.c</p>
<p>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</p>
<p>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]</p>
<p>Done Building Project “C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” (default targets) – FAILED.</p>
<p>Build FAILED.</p>
<p>“C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build\CMakeFiles\CMakeTmp\cmTC_338d1.vcxproj” (default target) (1) -&gt;</p>
<p>(ClCompile target) -&gt;</p>
<p>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]</p>
<pre><code>0 Warning(s)

1 Error(s)
</code></pre>
<p>Time Elapsed 00:00:01.07</p>

rtrahms: <p>It may not be pthreads, but a CUDA error.  Here is the console output:</p>
<p>C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build&gt;cmake -D CMAKE_PREFIX_PATH=c:\DATA\Developer\DeepLearning\pytorch\libtorch_win\libtorch -G “Visual Studio 15 2017 Win64”  …<br>
– Selecting Windows SDK version 10.0.17763.0 to target Windows 10.0.17134.<br>
– The C compiler identification is MSVC 19.16.27026.1<br>
– The CXX compiler identification is MSVC 19.16.27026.1<br>
– 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<br>
– 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<br>
– Detecting C compiler ABI info<br>
– Detecting C compiler ABI info - done<br>
– 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<br>
– 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<br>
– Detecting CXX compiler ABI info<br>
– Detecting CXX compiler ABI info - done<br>
– Detecting CXX compile features<br>
– Detecting CXX compile features - done<br>
– Looking for pthread.h<br>
– Looking for pthread.h - not found<br>
– Found Threads: TRUE<br>
– Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0 (found suitable version “9.0”, minimum required is “7.0”)<br>
– Caffe2: CUDA detected: 9.0<br>
– Caffe2: CUDA nvcc is: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/bin/nvcc.exe<br>
– Caffe2: CUDA toolkit directory: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0<br>
– Caffe2: Header version is: 9.0<br>
– Found CUDNN: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/include<br>
– 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)<br>
– Autodetected CUDA architecture(s): 6.1<br>
– Added CUDA NVCC flags for: -gencode;arch=compute_61,code=sm_61<br>
CMake Error at <strong>C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Caffe2/public/cuda.cmake:368 (message):</strong><br>
**  Unknown cmake build type:**<br>
Call Stack (most recent call first):<br>
C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Caffe2/Caffe2Config.cmake:88 (include)<br>
C:/DATA/Developer/DeepLearning/pytorch/libtorch_win/libtorch/share/cmake/Torch/TorchConfig.cmake:39 (find_package)<br>
CMakeLists.txt:4 (find_package)</p>
<p>– Configuring incomplete, errors occurred!<br>
See also “C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeOutput.log”.<br>
See also “C:/DATA/Developer/DeepLearning/pytorch/exampleTorchApp/build/CMakeFiles/CMakeError.log”.</p>
<p>C:\DATA\Developer\DeepLearning\pytorch\exampleTorchApp\build&gt;</p>

nathansegerlind: <p>It’s not a CUDA problem,  but rather a CMAKE_BUILD_TYPE problem…</p>
<p>try adding</p>
<p>-DCMAKE_BUILD_TYPE=Debug<br>
or<br>
-DCMAKE_BUILD_TYPE=Release</p>

eusouoandre: <p>My case was</p>
<p>Your installed Caffe2 version uses cuDNN but I cannot find the cuDNN<br>
libraries.  Please set the proper cuDNN prefixes and / or install cuDNN.</p>
<p>CuDNN was uninstalled by mistake days before.</p>

msminhas93: <p>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!</p>
<p><a href="https://towardsdatascience.com/setting-up-a-c-project-in-visual-studio-2019-with-libtorch-1-6-ad8a0e49e82c" class="onebox" target="_blank" rel="noopener nofollow ugc">https://towardsdatascience.com/setting-up-a-c-project-in-visual-studio-2019-with-libtorch-1-6-ad8a0e49e82c</a></p>