# How to link libtorch\_cuda.so instead of libtorch\_cpu.so

**URL:** <https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127>\
**Category:** C++\
**Created:** [November 29, 2022, 12:12pm UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127 "2022-11-29T12:12:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Larry\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/larry_li/32/34659_2.png) [@Larry\_Li](https://discuss.pytorch.org/u/Larry_Li)\
**Post date:** [November 29, 2022, 12:12pm UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127/1 "2022-11-29T12:12:03Z")

</div>

I installed pytorch from source, and cuda related libs were generated.

But when I compile the demo code, the libtorch\_cpu.so was linked instead of libtorch\_cuda.so.

How can I make use of the cuda libs?

root@b92d948aa67e:/workspace/code/example/build# ldd main  
linux-vdso.so.1 (0x00007fff79d57000)  
libc10.so =\> /workspace/code/mytorch/pytorch/build/lib/libc10.so (0x00007f76e87f9000)  
libtorch\_cpu.so =\> /workspace/code/mytorch/pytorch/build/lib/libtorch\_cpu.so (0x00007f76d2cce000)  
libpthread.so.0 =\> /lib/x86\_64-linux-gnu/libpthread.so.0 (0x00007f76d2ca5000)  
libstdc++.so.6 =\> /lib/x86\_64-linux-gnu/libstdc++.so.6 (0x00007f76d2ac3000)  
libgcc\_s.so.1 =\> /lib/x86\_64-linux-gnu/libgcc\_s.so.1 (0x00007f76d2aa8000)  
libc.so.6 =\> /lib/x86\_64-linux-gnu/libc.so.6 (0x00007f76d28b6000)  
libm.so.6 =\> /lib/x86\_64-linux-gnu/libm.so.6 (0x00007f76d2765000)  
libgomp.so.1 =\> /lib/x86\_64-linux-gnu/libgomp.so.1 (0x00007f76d2723000)  
/lib64/ld-linux-x86-64.so.2 (0x00007f76e8a77000)  
librt.so.1 =\> /lib/x86\_64-linux-gnu/librt.so.1 (0x00007f76d2719000)  
libdl.so.2 =\> /lib/x86\_64-linux-gnu/libdl.so.2 (0x00007f76d2713000)  
libmkl\_intel\_lp64.so.1 =\> /root/anaconda3/lib/libmkl\_intel\_lp64.so.1 (0x00007f76d1b74000)  
libmkl\_gnu\_thread.so.1 =\> /root/anaconda3/lib/libmkl\_gnu\_thread.so.1 (0x00007f76cffe7000)  
libmkl\_core.so.1 =\> /root/anaconda3/lib/libmkl\_core.so.1 (0x00007f76cbb79000)  
libcupti.so.11.6 =\> /usr/local/cuda/lib64/libcupti.so.11.6 (0x00007f76cb2cd000)  
libcudart.so.11.0 =\> /usr/local/cuda/lib64/libcudart.so.11.0 (0x00007f76cb029000)  
libutil.so.1 =\> /lib/x86\_64-linux-gnu/libutil.so.1 (0x00007f76cb024000)

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 29, 2022, 5:26pm UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127/2 "2022-11-29T17:26:58Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Larry\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/larry_li/32/34659_2.png) [@Larry\_Li](https://discuss.pytorch.org/u/Larry_Li)\
**Post date:** [November 30, 2022, 2:25am UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127/3 "2022-11-30T02:25:03Z")

</div>

my code is copied from official demo: [examples/cpp/mnist at main · pytorch/examples · GitHub](https://github.com/pytorch/examples/tree/main/cpp/mnist). And bellowing is CmakeLists.txt :

```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)

```

---

<div class="post-metadata">

**Author:** ![Larry\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/larry_li/32/34659_2.png) [@Larry\_Li](https://discuss.pytorch.org/u/Larry_Li)\
**Post date:** [November 30, 2022, 2:29am UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127/4 "2022-11-30T02:29:02Z")

</div>

The code built successfully by gcc, but not linked to libtorch\_cuda.so.  
When clang specified as compiler, build process failed.

---

<div class="post-metadata">

**Author:** ![Larry\_Li](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/larry_li/32/34659_2.png) [@Larry\_Li](https://discuss.pytorch.org/u/Larry_Li)\
**Post date:** [December 1, 2022, 8:37am UTC](https://discuss.pytorch.org/t/how-to-link-libtorch-cuda-so-instead-of-libtorch-cpu-so/167127/5 "2022-12-01T08:37:56Z")

</div>

I replace gcc to clang, it works.

root@2d6e6703be5a:/workspace/code/example/build# ldd main  
linux-vdso.so.1 (0x00007ffead5db000)  
_ **libtorch\_cuda.so** _ =\> /workspace/code/mytorch/pytorch/torch/lib/libtorch\_cuda.so (0x00007f04848a2000)  
libc10\_cuda.so =\> /workspace/code/mytorch/pytorch/torch/lib/libc10\_cuda.so (0x00007f0484782000)  
libc10.so =\> /workspace/code/mytorch/pytorch/torch/lib/libc10.so (0x00007f0484614000)  
libtorch.so =\> /workspace/code/mytorch/pytorch/torch/lib/libtorch.so (0x00007f048460f000)  
libtorch\_cpu.so =\> /workspace/code/mytorch/pytorch/torch/lib/libtorch\_cpu.so (0x00007f046f08f000)  
libpthread.so.0 =\> /usr/lib/x86\_64-linux-gnu/libpthread.so.0 (0x00007f046f065000)  
libstdc++.so.6 =\> /usr/lib/x86\_64-linux-gnu/libstdc++.so.6 (0x00007f046ee81000)  
libm.so.6 =\> /usr/lib/x86\_64-linux-gnu/libm.so.6 (0x00007f046ed32000)  
libgcc\_s.so.1 =\> /usr/lib/x86\_64-linux-gnu/libgcc\_s.so.1 (0x00007f046ed17000)  
libc.so.6 =\> /usr/lib/x86\_64-linux-gnu/libc.so.6 (0x00007f046eb25000)  
libcudart.so.11.0 =\> /usr/local/cuda/lib64/libcudart.so.11.0 (0x00007f046e881000)  
libcusparse.so.11 =\> /usr/local/cuda/lib64/libcusparse.so.11 (0x00007f045f436000)  
libcurand.so.10 =\> /usr/local/cuda/lib64/libcurand.so.10 (0x00007f0459846000)  
libnvToolsExt.so.1 =\> /usr/local/cuda/lib64/libnvToolsExt.so.1 (0x00007f045963c000)  
libdl.so.2 =\> /usr/lib/x86\_64-linux-gnu/libdl.so.2 (0x00007f0459636000)  
librt.so.1 =\> /usr/lib/x86\_64-linux-gnu/librt.so.1 (0x00007f045962c000)  
libcufft.so.10 =\> /usr/local/cuda/lib64/libcufft.so.10 (0x00007f0450b9f000)  
libcublas.so.11 =\> /usr/local/cuda/lib64/libcublas.so.11 (0x00007f044741f000)  
libcudnn.so.8 =\> /usr/lib/x86\_64-linux-gnu/libcudnn.so.8 (0x00007f04471f9000)  
/lib64/ld-linux-x86-64.so.2 (0x00007f04af111000)  
libgomp.so.1 =\> /usr/lib/x86\_64-linux-gnu/libgomp.so.1 (0x00007f04471b7000)  
libmkl\_intel\_lp64.so.1 =\> /root/anaconda3/lib/libmkl\_intel\_lp64.so.1 (0x00007f0446618000)  
libmkl\_gnu\_thread.so.1 =\> /root/anaconda3/lib/libmkl\_gnu\_thread.so.1 (0x00007f0444a8d000)  
libmkl\_core.so.1 =\> /root/anaconda3/lib/libmkl\_core.so.1 (0x00007f044061d000)  
libcupti.so.11.6 =\> /usr/local/cuda/lib64/libcupti.so.11.6 (0x00007f043fd71000)  
libcublasLt.so.11 =\> /usr/local/cuda/lib64/libcublasLt.so.11 (0x00007f042ad0b000)  
libutil.so.1 =\> /usr/lib/x86\_64-linux-gnu/libutil.so.1 (0x00007f042ad06000)
