Skip to content

Failed to link torch_library using cmake #38239

@vsuryamurthy

Description

@vsuryamurthy

🐛 Bug

When I compile a simple package (a for instance) using cmake, the exported ${TORCH_LIBRARIES}$ is as follows,

torch;torch_library;/usr/lib/libc10.so;/opt/cuda/lib/stubs/libcuda.so;/opt/cuda/lib/libnvrtc.so;/opt/cuda/lib/libnvToolsExt.so;/opt/cuda/lib64/libcudart.so;/usr/lib/libc10_cuda.so

Package a compiles successfully even if the linker fails to link torch_library.

The actual problem arises when I have another package (b), dependent on the already created package a. When I build, it fails with error

CMake Error at /home/vsury/dev/ros/pytorch-example/devel_isolated/a/share/a/cmake/aConfig.cmake:150 (message):
  Project 'b' tried to find library '-Wl,--no-as-needed,$<TARGET_FILE:torch>
  -Wl,--as-needed'.  The library is neither a target nor built/installed
  properly.  Did you compile project 'a'? Did you find_package() it before
  the subdirectory containing its code is included?

The reason has to do with torch_library because there is no library such as libtorch_library.so.

Looking further, it seems like that ${TORCH_LIBRARIES} is linked to ${Caffe2_MAIN_LIBS},

https://github.com/pytorch/pytorch/blob/master/cmake/TorchConfig.cmake.in#L41

and ${Caffe2_MAIN_LIBS} is linked to torch_library,

https://github.com/pytorch/pytorch/blob/master/cmake/Caffe2Config.cmake.in#L121

I do not understand the need for torch_library and removing it from $TORCH_LIBRARIES$ fixes the entire problem.

To Reproduce

Steps to reproduce the behavior:

  1. Create a package a with any script with torch functionalities (basically CMakeLists should contain find_package(Torch REQUIRED) and build target with torch libraries.
  2. Create another package b and make it depend on a.
  3. Compile package b to get the error.

Expected behavior

Successful build

Environment

Please copy and paste the output from our

  • PyTorch Version (e.g., 1.0): 1.5.0
  • OS (e.g., Linux): Archlinux
  • How you installed PyTorch (conda, pip, source): pacman -S
  • Build command you used (if compiling from source): cmake and make
  • Python version:
  • CUDA/cuDNN version:
  • GPU models and configuration:
  • Any other relevant information:

Additional context

cc @malfet @yf225 @glaringlee

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: buildBuild system issuesmodule: cppRelated to C++ APItriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions