Linkage problem when building program with libtorch on Raspberry PI

username: tot0k
cooked: <p>Hi <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>I’m writing a C++ program using libtorch and OpenCV.</p>
<p>Here is the output of my <code>CMakeLists.txt</code>, with the libraries versions :</p>
<pre><code class="lang-auto">-- Pytorch status :
--     libraries: torch;torch_library;/usr/lib/libc10.so
-- OpenCV library status :
--     version: 3.2.0
--     libraries: opencv_calib3d;opencv_core;opencv_features2d;opencv_flann;opencv_highgui;opencv_imgcodecs;opencv_imgproc;opencv_ml;opencv_objdetect;opencv_photo;opencv_shape;opencv_stitching;opencv_superres;opencv_video;opencv_videoio;opencv_videostab;opencv_viz;opencv_aruco;opencv_bgsegm;opencv_bioinspired;opencv_ccalib;opencv_datasets;opencv_dpm;opencv_face;opencv_freetype;opencv_fuzzy;opencv_hdf;opencv_line_descriptor;opencv_optflow;opencv_phase_unwrapping;opencv_plot;opencv_reg;opencv_rgbd;opencv_saliency;opencv_stereo;opencv_structured_light;opencv_surface_matching;opencv_text;opencv_ximgproc;opencv_xobjdetect;opencv_xphoto
--     include path: /usr/include;/usr/include/opencv
-- Configuring done
-- Generating done
-- Build files have been written to: /home/&lt;my_user&gt;/&lt;project&gt;/cmake-build
</code></pre>
<p>You can see that torch needs <code>libc10.so</code> to work.</p>
<p>When I call <code>make</code>, everything goes fine, until this happens during linking :</p>
<pre><code class="lang-auto">[  8%] Linking CXX executable POC_V4
/usr/lib/libc10.so: file not recognized: file format not recognized
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/POC_V4.dir/build.make:298: POC_V4] Error 1
make[1]: *** [CMakeFiles/Makefile2:96: CMakeFiles/POC_V4.dir/all] Error 2
make: *** [Makefile:104: all] Error 2
</code></pre>
<p>When I do a <code>file</code> command on this file, I see that it is compiled for x86-64 architectures, instead of ARM.<br>
Here is the output of the <code>file</code> command on <code>/usr/lib/libc10.so</code> and on <code>/usr/lib/libtorch.so.3.0.0</code> to have a comparison:</p>
<pre><code class="lang-auto">/usr/lib/libc10.so: ELF 64-bit LSB pie executable, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=f8019a46a003bc238af0fb3fb8331af8af69aa33, not stripped
/usr/lib/libtorch.so.3.0.0: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=2f8320292f01aab7c9e2e2577b3be2025689c0a7, stripped
</code></pre>
<p>How can I have a working libc10.so file ?</p>
<p>I found <a href="https://github.com/pytorch/pytorch/issues/15138" rel="nofollow noopener">this issue</a> with a similar problem, I am currently trying to build PyTorch from source, but since it takes a really long time to build on Rasberry PI, I write this post here to find possible other solutions if this one doesn’t work.</p>
<p>Thanks for reading, I hope someone will be able to help me <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p><strong>EDIT:</strong></p>
<p>I started to re-build PyTorch on my raspberry PI so it would re-build libtorch too.<br>
This takes many hours, and crashed during the night for a linkage problem :</p>
<pre><code class="lang-auto">/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/conv_to_nnpack_transform_test.dir/build.make:108: bin/conv_to_nnpack_transform_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2836: caffe2/CMakeFiles/conv_to_nnpack_transform_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/depthwise3x3_conv_op_test.dir/build.make:108: bin/depthwise3x3_conv_op_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2867: caffe2/CMakeFiles/depthwise3x3_conv_op_test.dir/all] Error 2
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/&lt;myuser&gt;/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_compare_exchange_8'
collect2: error: ld returned 1 exit status
make[2]: *** [caffe2/CMakeFiles/pattern_net_transform_test.dir/build.make:108: bin/pattern_net_transform_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:2805: caffe2/CMakeFiles/pattern_net_transform_test.dir/all] Error 2
make: *** [Makefile:161: all] Error 2
</code></pre>
<p>However, the build outputed a <code>libc10.so</code> file with the following signature!</p>
<pre><code class="lang-auto">/usr/lib/libc10.so: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=61d77cfaa9647739e17f6b08caeb53382d9e9108, not stripped
</code></pre>
<p>I replaced it in the <code>/usr/lib</code> folder and tried to build my project : It went further than before, and crashed for another file-format problem. This proves that if I manage to fully rebuild libtorch, I should be able to build my project !</p>
<p>Can someone help me solve the new linking problem ?</p>