Hi I’ve got this code:
#include <torch/torch.h>
#include <iostream>
int main() {
std::cout << torch::cuda::is_available() << "," << torch::cuda::device_count() << std::endl;
return 0;
}
and when I compile it with g++ I get the following error:
$ g++ -I/usr/include/torch/csrc/api/include -L/usr/lib test.cpp -o test
/usr/bin/ld: /tmp/ccZEla7R.o: warning: relocation against `_ZN3c1019UndefinedTensorImpl10_singletonE' in read-only section `.text._ZN3c1019UndefinedTensorImpl9singletonEv[_ZN3c1019UndefinedTensorImpl9singletonEv]'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `main':
test.cpp:(.text+0xa): undefined reference to `torch::cuda::is_available()'
/usr/bin/ld: test.cpp:(.text+0x3b): undefined reference to `torch::cuda::device_count()'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, c10::detail::CompileTimeEmptyString)':
test.cpp:(.text._ZN3c106detail23torchInternalAssertFailEPKcS2_jS2_NS0_22CompileTimeEmptyStringE[_ZN3c106detail23torchInternalAssertFailEPKcS2_jS2_NS0_22CompileTimeEmptyStringE]+0x2a): undefined reference to `c10::detail::torchCheckFail(char const*, char const*, unsigned int, char const*)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::SymInt::SymInt(long)':
test.cpp:(.text._ZN3c106SymIntC2El[_ZN3c106SymIntC5El]+0x41): undefined reference to `c10::SymInt::promote_to_negative()'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::UndefinedTensorImpl::singleton()':
test.cpp:(.text._ZN3c1019UndefinedTensorImpl9singletonEv[_ZN3c1019UndefinedTensorImpl9singletonEv]+0x7): undefined reference to `c10::UndefinedTensorImpl::_singleton'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::isIntrusivePtr() const':
test.cpp:(.text._ZNK3c106IValue14isIntrusivePtrEv[_ZNK3c106IValue14isIntrusivePtrEv]+0x1b3): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::toComplexDouble() const':
test.cpp:(.text._ZNK3c106IValue15toComplexDoubleEv[_ZNK3c106IValue15toComplexDoubleEv]+0xb1): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::toTensor() &&':
test.cpp:(.text._ZNO3c106IValue8toTensorEv[_ZNO3c106IValue8toTensorEv]+0x34): undefined reference to `c10::IValue::reportToTensorTypeError() const'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::toTensor() const &':
test.cpp:(.text._ZNKR3c106IValue8toTensorEv[_ZNKR3c106IValue8toTensorEv]+0x30): undefined reference to `c10::IValue::reportToTensorTypeError() const'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::toSymInt() const &':
test.cpp:(.text._ZNKR3c106IValue8toSymIntEv[_ZNKR3c106IValue8toSymIntEv]+0xd8): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: test.cpp:(.text._ZNKR3c106IValue8toSymIntEv[_ZNKR3c106IValue8toSymIntEv]+0x10e): undefined reference to `c10::SymInt::SymInt(c10::intrusive_ptr<c10::SymNodeImpl, c10::detail::intrusive_target_default_null_type<c10::SymNodeImpl> >)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::intrusive_ptr<c10::SymNodeImpl, c10::detail::intrusive_target_default_null_type<c10::SymNodeImpl> >::reclaim(c10::SymNodeImpl*)':
test.cpp:(.text._ZN3c1013intrusive_ptrINS_11SymNodeImplENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_[_ZN3c1013intrusive_ptrINS_11SymNodeImplENS_6detail34intrusive_target_default_null_typeIS1_EEE7reclaimEPS1_]+0x16d): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::intrusive_ptr<c10::intrusive_ptr_target, c10::UndefinedTensorImpl>::reclaim(c10::intrusive_ptr_target*)':
test.cpp:(.text._ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_19UndefinedTensorImplEE7reclaimEPS1_[_ZN3c1013intrusive_ptrINS_20intrusive_ptr_targetENS_19UndefinedTensorImplEE7reclaimEPS1_]+0x16d): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::intrusive_ptr<c10::TensorImpl, c10::UndefinedTensorImpl>::retain_()':
test.cpp:(.text._ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev[_ZN3c1013intrusive_ptrINS_10TensorImplENS_19UndefinedTensorImplEE7retain_Ev]+0x80): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::intrusive_ptr<c10::ivalue::ComplexHolder, c10::detail::intrusive_target_default_null_type<c10::ivalue::ComplexHolder> >::reclaim(c10::ivalue::ComplexHolder*)':
test.cpp:(.text._ZN3c1013intrusive_ptrINS_6ivalue13ComplexHolderENS_6detail34intrusive_target_default_null_typeIS2_EEE7reclaimEPS2_[_ZN3c1013intrusive_ptrINS_6ivalue13ComplexHolderENS_6detail34intrusive_target_default_null_typeIS2_EEE7reclaimEPS2_]+0x16d): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, char const*)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::SmallVectorTemplateCommon<long, void>::grow_pod(unsigned long, unsigned long)':
test.cpp:(.text._ZN3c1025SmallVectorTemplateCommonIlvE8grow_podEmm[_ZN3c1025SmallVectorTemplateCommonIlvE8grow_podEmm]+0x3a): undefined reference to `c10::SmallVectorBase<unsigned int>::grow_pod(void*, unsigned long, unsigned long)'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
.
I have
$ sudo find / -name "*libtorch*"
[sudo] password for vorlket:
/var/lib/pacman/local/libtorch-cuda-2.1.0-1
/home/vorlket/build/libtorch-cuda
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorchbind_test.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch_cuda.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch_global_deps.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch_cpu.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch_python.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch_cuda_linalg.so
/home/vorlket/build/libtorch-cuda/pkg/libtorch-cuda/usr/lib/libtorch.so
/home/vorlket/build/libtorch-cuda/libtorch-cuda-2.1.0-1-x86_64.pkg.tar.zst
/home/vorlket/build/libtorch-cuda/libtorch-cuda-2.1.0.zip
/home/vorlket/libtorchprac
/usr/lib/libtorchbind_test.so
/usr/lib/libtorch_cpu.so
/usr/lib/libtorch_global_deps.so
/usr/lib/libtorch_cuda_linalg.so
/usr/lib/libtorch_python.so
/usr/lib/libtorch_cuda.so
/usr/lib/libtorch.so
Could someone please help me finding out why the linker fails and how to resolve it?