Linkage problem when building program with libtorch on Raspberry PI

Hi :slight_smile:

I’m writing a C++ program using libtorch and OpenCV.

Here is the output of my CMakeLists.txt, with the libraries versions :

-- 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/<my_user>/<project>/cmake-build

You can see that torch needs libc10.so to work.

When I call make, everything goes fine, until this happens during linking :

[  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

When I do a file command on this file, I see that it is compiled for x86-64 architectures, instead of ARM.
Here is the output of the file command on /usr/lib/libc10.so and on /usr/lib/libtorch.so.3.0.0 to have a comparison:

/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

How can I have a working libc10.so file ?

I found this issue 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.

Thanks for reading, I hope someone will be able to help me :slight_smile:

EDIT:

I started to re-build PyTorch on my raspberry PI so it would re-build libtorch too.
This takes many hours, and crashed during the night for a linkage problem :

/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/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/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/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/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_add_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_load_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_exchange_8'
/usr/bin/ld: /home/<myuser>/pytorch_install/pytorch/build/lib/libcaffe2.so: undefined reference to `__atomic_fetch_sub_8'
/usr/bin/ld: /home/<myuser>/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

However, the build outputed a libc10.so file with the following signature!

/usr/lib/libc10.so: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=61d77cfaa9647739e17f6b08caeb53382d9e9108, not stripped

I replaced it in the /usr/lib 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 !

Can someone help me solve the new linking problem ?