Libtorch program not compiling

vorlket: <p>Hi I’ve got this code:</p>
<pre><code class="lang-auto">#include &lt;torch/torch.h&gt;
#include &lt;iostream&gt;

int main() {
        std::cout &lt;&lt; torch::cuda::is_available() &lt;&lt; "," &lt;&lt; torch::cuda::device_count() &lt;&lt; std::endl;
        return 0;
}</code></pre>
<p>and when I compile it with g++ I get the following error:</p>
<p><code>
$ 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&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/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&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::IValue::toTensor() &amp;&amp;':
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 &amp;':
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 &amp;':
test.cpp:(.text._ZNKR3c106IValue8toSymIntEv[_ZNKR3c106IValue8toSymIntEv]+0xd8): undefined reference to `c10::detail::torchInternalAssertFail(char const*, char const*, unsigned int, char const*, std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
/usr/bin/ld: test.cpp:(.text._ZNKR3c106IValue8toSymIntEv[_ZNKR3c106IValue8toSymIntEv]+0x10e): undefined reference to `c10::SymInt::SymInt(c10::intrusive_ptr&lt;c10::SymNodeImpl, c10::detail::intrusive_target_default_null_type&lt;c10::SymNodeImpl&gt; &gt;)'
/usr/bin/ld: /tmp/ccZEla7R.o: in function `c10::intrusive_ptr&lt;c10::SymNodeImpl, c10::detail::intrusive_target_default_null_type&lt;c10::SymNodeImpl&gt; &gt;::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&lt;c10::intrusive_ptr_target, c10::UndefinedTensorImpl&gt;::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&lt;c10::TensorImpl, c10::UndefinedTensorImpl&gt;::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&lt;c10::ivalue::ComplexHolder, c10::detail::intrusive_target_default_null_type&lt;c10::ivalue::ComplexHolder&gt; &gt;::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&lt;long, void&gt;::grow_pod(unsigned long, unsigned long)':
test.cpp:(.text._ZN3c1025SmallVectorTemplateCommonIlvE8grow_podEmm[_ZN3c1025SmallVectorTemplateCommonIlvE8grow_podEmm]+0x3a): undefined reference to `c10::SmallVectorBase&lt;unsigned int&gt;::grow_pod(void*, unsigned long, unsigned long)'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status
</code>.</p>
<p>I have</p>
<pre><code class="lang-auto">$ 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</code></pre>
<p>Could someone please help me finding out why the linker fails and how to resolve it?</p>

vorlket: <p>If I use <code>$ g++ -I/usr/include/torch/csrc/api/include -L/usr/lib -ltorch_cuda -lc10 -ltorch_cpu test.cpp -o test</code>, it compiles, but when I run the program it produces the following:</p>
<pre><code class="lang-auto">$ ./test
./test: error while loading shared libraries: libtorch_cuda.so: ELF load command address/offset not page-aligned</code></pre>

SunBurntBadly_0: <p>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: <a href="https://termbin.com/ixpp" rel="noopener nofollow ugc">https://termbin.com/ixpp</a> I have not tried -ltorch_cpu flag yet<br>
`</p>

SunBurntBadly_0: <p>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</p>

mamsds: <p>My installation could be a bit different so my complication command is this:</p>
<pre data-code-wrap="bash"><code class="lang-bash">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
</code></pre>
<p>Note that I used <code>-lc10_cuda</code> instead of <code>-ltorch_cuda</code>, not sure how it could impact the result.<br>
Then I get this:</p>
<pre data-code-wrap="bash"><code class="lang-bash">$ ./test 
./test: error while loading shared libraries: libc10.so: cannot open shared object file: No such file or directory
</code></pre>
<p>So the shared object I miss is different. I just <code>find</code> it and add the path to <code>$LD_LIBRARY_PATH</code>:</p>
<pre data-code-wrap="bash"><code class="lang-bash">$ find / -name libc10.so 2&gt;/dev/null 
/usr/local/lib/torch/libc10.so

$ export LD_LIBRARY_PATH=/usr/local/lib/torch/:$LD_LIBRARY_PATH
</code></pre>
<p>and then it works:</p>
<pre data-code-wrap="bash"><code class="lang-bash">$ ./test 
1,1
</code></pre>

vorlket: <p>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.</p>