Linker Error with basic C++ Example

Following https://pytorch.org/cppdocs/installing.html.

~/example-app/build$ ~/Downloads/cmake-3.15.2-Linux-x86_64/bin/cmake -DCMAKE_PREFIX_PATH=/home/user/anaconda3/envs/th/lib/python3.6/site-packages/torch/share/cmake/Torch -D_GLIBCXX_USE_CXX11_ABI=0 ..
-- Caffe2: CUDA detected: 9.0
-- Caffe2: CUDA nvcc is: /usr/local/cuda/bin/nvcc
-- Caffe2: CUDA toolkit directory: /usr/local/cuda
-- Caffe2: Header version is: 9.0
-- Found cuDNN: v7.1.2  (include: /home/user/anaconda3/envs/th/include, library: /home/user/anaconda3/envs/th/lib/libcudnn.so)
-- Autodetected CUDA architecture(s):  6.1
-- Added CUDA NVCC flags for: -gencode;arch=compute_61,code=sm_61
-- Configuring done
CMake Warning at CMakeLists.txt:6 (add_executable):
  Cannot generate a safe runtime search path for target example-app because
  there is a cycle in the constraint graph:

    dir 0 is [/home/user/anaconda3/envs/th/lib/python3.6/site-packages/torch/lib]
    dir 1 is [/usr/local/cuda-9.0/lib64/stubs]
    dir 2 is [/home/user/anaconda3/envs/th/lib]
      dir 3 must precede it due to runtime library [libcudart.so.9.0]
    dir 3 is [/usr/local/cuda/lib64]
      dir 2 must precede it due to runtime library [libnvrtc.so.9.0]

  Some of these libraries may not be found correctly.


-- Generating done
-- Build files have been written to: /home/user/example-app/build
CMakeFiles/example-app.dir/example-app.cpp.o: In function `torch::jit::Graph::insertNode(torch::jit::Node*)':
example-app.cpp:(.text._ZN5torch3jit5Graph10insertNodeEPNS0_4NodeE[_ZN5torch3jit5Graph10insertNodeEPNS0_4NodeE]+0xf7): undefined reference to `torch::jit::Node::insertBefore(torch::jit::Node*)'
CMakeFiles/example-app.dir/example-app.cpp.o: In function `torch::jit::tracer::isTracing()':
example-app.cpp:(.text._ZN5torch3jit6tracer9isTracingEv[_ZN5torch3jit6tracer9isTracingEv]+0x5): undefined reference to `torch::jit::tracer::getTracingState()'
CMakeFiles/example-app.dir/example-app.cpp.o: In function `torch::rand(c10::ArrayRef<long>, c10::TensorOptions const&)':
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x66): undefined reference to `torch::jit::tracer::getTracingState()'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0xf4): undefined reference to `torch::jit::Graph::create(c10::Symbol, unsigned long)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x104): undefined reference to `torch::jit::tracer::recordSourceLocation(torch::jit::Node*)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x126): undefined reference to `torch::jit::tracer::addInputs(torch::jit::Node*, char const*, c10::ArrayRef<long>)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x13e): undefined reference to `torch::jit::tracer::addInputs(torch::jit::Node*, char const*, c10::TensorOptions const&)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x185): undefined reference to `torch::jit::tracer::setTracingState(std::shared_ptr<torch::jit::tracer::TracingState>)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x2a6): undefined reference to `torch::jit::tracer::setTracingState(std::shared_ptr<torch::jit::tracer::TracingState>)'
example-app.cpp:(.text._ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE[_ZN5torch4randEN3c108ArrayRefIlEERKNS0_13TensorOptionsE]+0x2c8): undefined reference to `torch::jit::tracer::addOutput(torch::jit::Node*, at::Tensor const&)'
CMakeFiles/example-app.dir/example-app.cpp.o: In function `torch::jit::script::SimpleValue::SimpleValue(torch::jit::Value*)':
example-app.cpp:(.text._ZN5torch3jit6script11SimpleValueC2EPNS0_5ValueE[_ZN5torch3jit6script11SimpleValueC5EPNS0_5ValueE]+0x1d): undefined reference to `vtable for torch::jit::script::SimpleValue'
CMakeFiles/example-app.dir/example-app.cpp.o: In function `c10::intrusive_ptr<torch::autograd::Variable::Impl, c10::detail::intrusive_target_default_null_type<torch::autograd::Variable::Impl> > c10::intrusive_ptr<torch::autograd::Variable::Impl, c10::detail::intrusive_target_default_null_type<torch::autograd::Variable::Impl> >::make<at::Tensor, std::unique_ptr<torch::autograd::Variable::AutogradMeta, std::default_delete<torch::autograd::Variable::AutogradMeta> >, bool&>(at::Tensor&&, std::unique_ptr<torch::autograd::Variable::AutogradMeta, std::default_delete<torch::autograd::Variable::AutogradMeta> >&&, bool&)':
example-app.cpp:(.text._ZN3c1013intrusive_ptrIN5torch8autograd8Variable4ImplENS_6detail34intrusive_target_default_null_typeIS4_EEE4makeIJN2at6TensorESt10unique_ptrINS3_12AutogradMetaESt14default_deleteISD_EERbEEES8_DpOT_[_ZN3c1013intrusive_ptrIN5torch8autograd8Variable4ImplENS_6detail34intrusive_target_default_null_typeIS4_EEE4makeIJN2at6TensorESt10unique_ptrINS3_12AutogradMetaESt14default_deleteISD_EERbEEES8_DpOT_]+0xc0): undefined reference to `torch::autograd::Variable::Impl::Impl(at::Tensor, std::unique_ptr<torch::autograd::Variable::AutogradMeta, std::default_delete<torch::autograd::Variable::AutogradMeta> >, bool, torch::autograd::Edge)'
collect2: error: ld returned 1 exit status
CMakeFiles/example-app.dir/build.make:100: recipe for target 'example-app' failed
make[2]: *** [example-app] Error 1
CMakeFiles/Makefile2:75: recipe for target 'CMakeFiles/example-app.dir/all' failed
make[1]: *** [CMakeFiles/example-app.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

The files are the same as in the tutorial.
Using Ubuntu 16.04.

1 Like

The error was in my LD_LIBRARY_PATH. It was missing /usr/lib for some reason. Also, I had an issue where I had the flag -Wl,--whole-archive.