How to link libtorch_cuda.so instead of libtorch_cpu.so

username: Larry_Li
cooked: <p>I installed pytorch from source, and cuda related libs were generated.</p>
<p>But when I compile the demo code, the libtorch_cpu.so was linked instead of libtorch_cuda.so.</p>
<p>How can I make use of the cuda libs?</p>
<p>root@b92d948aa67e:/workspace/code/example/build# ldd main<br>
linux-vdso.so.1 (0x00007fff79d57000)<br>
libc10.so =&gt; /workspace/code/mytorch/pytorch/build/lib/libc10.so (0x00007f76e87f9000)<br>
libtorch_cpu.so =&gt; /workspace/code/mytorch/pytorch/build/lib/libtorch_cpu.so (0x00007f76d2cce000)<br>
libpthread.so.0 =&gt; /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f76d2ca5000)<br>
libstdc++.so.6 =&gt; /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f76d2ac3000)<br>
libgcc_s.so.1 =&gt; /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f76d2aa8000)<br>
libc.so.6 =&gt; /lib/x86_64-linux-gnu/libc.so.6 (0x00007f76d28b6000)<br>
libm.so.6 =&gt; /lib/x86_64-linux-gnu/libm.so.6 (0x00007f76d2765000)<br>
libgomp.so.1 =&gt; /lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f76d2723000)<br>
/lib64/ld-linux-x86-64.so.2 (0x00007f76e8a77000)<br>
librt.so.1 =&gt; /lib/x86_64-linux-gnu/librt.so.1 (0x00007f76d2719000)<br>
libdl.so.2 =&gt; /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f76d2713000)<br>
libmkl_intel_lp64.so.1 =&gt; /root/anaconda3/lib/libmkl_intel_lp64.so.1 (0x00007f76d1b74000)<br>
libmkl_gnu_thread.so.1 =&gt; /root/anaconda3/lib/libmkl_gnu_thread.so.1 (0x00007f76cffe7000)<br>
libmkl_core.so.1 =&gt; /root/anaconda3/lib/libmkl_core.so.1 (0x00007f76cbb79000)<br>
libcupti.so.11.6 =&gt; /usr/local/cuda/lib64/libcupti.so.11.6 (0x00007f76cb2cd000)<br>
libcudart.so.11.0 =&gt; /usr/local/cuda/lib64/libcudart.so.11.0 (0x00007f76cb029000)<br>
libutil.so.1 =&gt; /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f76cb024000)</p>

username: ptrblck
cooked: <p>I would expect the build steps to link against the needed cuda libraries, if any CUDA code is used in your application. Since it’s not linking against it I guess your code might be using the CPU only?</p>

username: Larry_Li
cooked: <p>my code is copied from official demo: <a href="https://github.com/pytorch/examples/tree/main/cpp/mnist" class="inline-onebox" rel="noopener nofollow ugc">examples/cpp/mnist at main · pytorch/examples · GitHub</a>. And bellowing is CmakeLists.txt :</p>
<pre><code class="lang-auto">
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)

project(main)

set(CMAKE_INSTALL_RPATH "/workspace/code/mytorch/pytorch/build/lib/")

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -g")

add_executable(main main.cpp)

target_include_directories(main PUBLIC

"/workspace/code/mytorch/pytorch/torch/csrc/api/include"

# "/workspace/code/mytorch/pytorch/"

"/workspace/code/mytorch/pytorch/torch/include"

)

target_link_libraries(main

"/workspace/code/mytorch/pytorch/build/lib/libtorch_cuda.so"

"/workspace/code/mytorch/pytorch/build/lib/libc10_cuda.so"

"/workspace/code/mytorch/pytorch/build/lib/libc10.so"

"/workspace/code/mytorch/pytorch/build/lib/libtorch.so"

"/workspace/code/mytorch/pytorch/build/lib/libtorch_cpu.so"

"/lib/x86_64-linux-gnu/libpthread.so.0"

#"/lib/x86_64-linux-gnu/libm.so"

#"/usr/lib/x86_64-linux-gnu/libdl.so"

)

set_property(TARGET main PROPERTY CXX_STANDARD 14)
</code></pre>

username: Larry_Li
cooked: <p>The code built successfully by gcc, but not linked to libtorch_cuda.so.<br>
When clang specified as compiler, build process failed.</p>

username: Larry_Li
cooked: <p>I replace gcc to clang, it works.</p>
<p>root@2d6e6703be5a:/workspace/code/example/build# ldd main<br>
linux-vdso.so.1 (0x00007ffead5db000)<br>
<em><strong>libtorch_cuda.so</strong></em> =&gt; /workspace/code/mytorch/pytorch/torch/lib/libtorch_cuda.so (0x00007f04848a2000)<br>
libc10_cuda.so =&gt; /workspace/code/mytorch/pytorch/torch/lib/libc10_cuda.so (0x00007f0484782000)<br>
libc10.so =&gt; /workspace/code/mytorch/pytorch/torch/lib/libc10.so (0x00007f0484614000)<br>
libtorch.so =&gt; /workspace/code/mytorch/pytorch/torch/lib/libtorch.so (0x00007f048460f000)<br>
libtorch_cpu.so =&gt; /workspace/code/mytorch/pytorch/torch/lib/libtorch_cpu.so (0x00007f046f08f000)<br>
libpthread.so.0 =&gt; /usr/lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f046f065000)<br>
libstdc++.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f046ee81000)<br>
libm.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libm.so.6 (0x00007f046ed32000)<br>
libgcc_s.so.1 =&gt; /usr/lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f046ed17000)<br>
libc.so.6 =&gt; /usr/lib/x86_64-linux-gnu/libc.so.6 (0x00007f046eb25000)<br>
libcudart.so.11.0 =&gt; /usr/local/cuda/lib64/libcudart.so.11.0 (0x00007f046e881000)<br>
libcusparse.so.11 =&gt; /usr/local/cuda/lib64/libcusparse.so.11 (0x00007f045f436000)<br>
libcurand.so.10 =&gt; /usr/local/cuda/lib64/libcurand.so.10 (0x00007f0459846000)<br>
libnvToolsExt.so.1 =&gt; /usr/local/cuda/lib64/libnvToolsExt.so.1 (0x00007f045963c000)<br>
libdl.so.2 =&gt; /usr/lib/x86_64-linux-gnu/libdl.so.2 (0x00007f0459636000)<br>
librt.so.1 =&gt; /usr/lib/x86_64-linux-gnu/librt.so.1 (0x00007f045962c000)<br>
libcufft.so.10 =&gt; /usr/local/cuda/lib64/libcufft.so.10 (0x00007f0450b9f000)<br>
libcublas.so.11 =&gt; /usr/local/cuda/lib64/libcublas.so.11 (0x00007f044741f000)<br>
libcudnn.so.8 =&gt; /usr/lib/x86_64-linux-gnu/libcudnn.so.8 (0x00007f04471f9000)<br>
/lib64/ld-linux-x86-64.so.2 (0x00007f04af111000)<br>
libgomp.so.1 =&gt; /usr/lib/x86_64-linux-gnu/libgomp.so.1 (0x00007f04471b7000)<br>
libmkl_intel_lp64.so.1 =&gt; /root/anaconda3/lib/libmkl_intel_lp64.so.1 (0x00007f0446618000)<br>
libmkl_gnu_thread.so.1 =&gt; /root/anaconda3/lib/libmkl_gnu_thread.so.1 (0x00007f0444a8d000)<br>
libmkl_core.so.1 =&gt; /root/anaconda3/lib/libmkl_core.so.1 (0x00007f044061d000)<br>
libcupti.so.11.6 =&gt; /usr/local/cuda/lib64/libcupti.so.11.6 (0x00007f043fd71000)<br>
libcublasLt.so.11 =&gt; /usr/local/cuda/lib64/libcublasLt.so.11 (0x00007f042ad0b000)<br>
libutil.so.1 =&gt; /usr/lib/x86_64-linux-gnu/libutil.so.1 (0x00007f042ad06000)</p>