How to link libtorch_cuda.so instead of libtorch_cpu.so

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)

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?

my code is copied from official demo: examples/cpp/mnist at main · pytorch/examples · GitHub. And bellowing is CmakeLists.txt :


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)

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

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)