Libtorch program not compiling

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?

If I use $ g++ -I/usr/include/torch/csrc/api/include -L/usr/lib -ltorch_cuda -lc10 -ltorch_cpu test.cpp -o test, it compiles, but when I run the program it produces the following:

$ ./test
./test: error while loading shared libraries: libtorch_cuda.so: ELF load command address/offset not page-aligned

Hello, I am also getting something similar as well but I am using the cpu-only libtorch folders. Everything is included (path to libtorch) and I aslo try to link it. This is what I am getting: https://termbin.com/ixpp I have not tried -ltorch_cpu flag yet
`

Nevermind, I got mine to work. I just needed those flags that you used. So, you should try actually linking towards the library and not /usr/lib You’re linking to your /usr/lib which I am guessing maybe libtorch is not installed inside there. And you should actually link it something like this: -L /path/to/libtorch/libtorch

My installation could be a bit different so my complication command is this:

g++ -I/usr/local/include/torch/torch/csrc/api/include/ -I/usr/local/include/torch/ -L/usr/local/lib/torch/ test.cpp -o test -lc10 -lc10_cuda -ltorch_cpu

Note that I used -lc10_cuda instead of -ltorch_cuda, not sure how it could impact the result.
Then I get this:

$ ./test 
./test: error while loading shared libraries: libc10.so: cannot open shared object file: No such file or directory

So the shared object I miss is different. I just find it and add the path to $LD_LIBRARY_PATH:

$ find / -name libc10.so 2>/dev/null 
/usr/local/lib/torch/libc10.so

$ export LD_LIBRARY_PATH=/usr/local/lib/torch/:$LD_LIBRARY_PATH

and then it works:

$ ./test 
1,1

In my case, all the libraries are in /usr/lib. I tried -lc10_cuda and it made no difference. I’m using archlinux and libtorch-cuda. My problem is that libtorch_cuda.so is broken like above somehow.