C++ static lib on windows cannot find CUDA

tkr: <p>Hi,</p>
<p>I am trying to use libtorch with static build.<br>
(using Visual Studio 2017/CUDA10.1, source is v1.5.0)<br>
It seems building static libtorch is succeeding but torch could not find CUDA device.</p>
<pre><code class="lang-auto">std::cout &lt;&lt; torch::cuda::is_available() &lt;&lt; std::endl;
try {
	torch::Tensor tensor = torch::randn({ 1,1 });
	tensor.to(torch::Device("cuda:0"));
}
catch (std::exception&amp; ex) {
	std::cout &lt;&lt; ex.what() &lt;&lt; std::endl;
}
</code></pre>
<blockquote>
<p>0<br>
Cannot initialize CUDA without ATen_cuda library. PyTorch splits its backend into two shared libraries: a CPU library and a CUDA library; this error has occurred because you are trying to use some CUDA functionality, but the CUDA library has not been loaded by the dynamic linker for some reason.  The CUDA library MUST be loaded, EVEN IF you don’t directly use any symbols from the CUDA library! One common culprit is a lack of -Wl,–no-as-needed in your link arguments; many dynamic linkers will delete dynamic library dependencies if you don’t depend on any of their symbols.  You can check if this has occurred by using ldd on your binary to see if there is a dependency on *_cuda.so library. (initCUDA at C:\…\pytorch\aten\src\ATen/detail/CUDAHooksInterface.h:63)<br>
(no backtrace available)</p>
</blockquote>
<p>What is “ATen_cuda library” ?<br>
What should I do ?</p>
<p>My libtorch creation process is following.</p>
<pre><code class="lang-auto">set BUILD_SHARED_LIBS=OFF
set BUILD_TEST=OFF
set BUILD_CAFFE2_OPS=OFF   (If set ON, LNK1248 occurs)
set BUILD_PYTHON=False
set CAFFE2_USE_MSVC_STATIC_RUNTIME=ON
set CAFFE2_STATIC_LINK_CUDA=ON
set CUDNN_STATIC=ON
set MSVC_Z7_OVERRIDE=OFF
set USE_CUDA=ON
set USE_CUDNN=ON
set USE_FBGEMM=OFF

py tools\build_libtorch.py
</code></pre>
<p>The simple application above links with all created static libs (under pytorch/biuld/libs).</p>
<pre><code class="lang-auto">Caffe2_perfkernels_avx.lib
Caffe2_perfkernels_avx2.lib
Caffe2_perfkernels_avx512.lib
c10.lib
c10_cuda.lib
caffe2_protos.lib
clog.lib
cpuinfo.lib
cpuinfo_internals.lib
foxi_loader.lib
libprotobuf-lite.lib
libprotobuf.lib
libprotoc.lib
mkldnn.lib
onnx.lib
onnx_proto.lib
torch.lib
torch_cpu.lib
torch_cuda.lib

/WHOLEARCHIVE:torch_cpu.lib
</code></pre>
<p>I tried to build with “/WHOLEARCHIVE:torch_cuda.lib”, but LNK1102 (out of memory) occured.</p>
<p>I also tried linking below, but the result is not changed.</p>
<pre><code class="lang-auto">C:\Program Files\NVIDIA Corporation\NvToolsExt\lib\x64\nvToolsExt64_1.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudart_static.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cufft.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\curand.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cublas.lib
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1\lib\x64\cudnn.lib
</code></pre>
<p>Do you have any suggestion ?<br>
Thanks.</p>

peterjc123: <p><code>/WHOLEARCHIVE:torch_cuda.lib</code> is the right choice. But you could try building with the x64 toolchain. You may get more details in <a href="https://stackoverflow.com/questions/19820718/how-to-make-visual-studio-use-the-native-amd64-toolchain" rel="nofollow noopener">https://stackoverflow.com/questions/19820718/how-to-make-visual-studio-use-the-native-amd64-toolchain</a>.</p>

tkr: <p><a class="mention" href="/u/peterjc123">@peterjc123</a><br>
Thanks to the solution you provided, the LNK1102 (out of memory) issue has been resolved.<br>
And the sample code above is worked correctly building with following settings:</p>
<ul>
<li>/WHOLEARCHIVE: torch_cpu.lib  torch_cuda.lib c10_cuda.lib</li>
<li>link cuda, cudnn libs</li>
</ul>
<p>Thx !</p>