在 Raspberry PI 上使用 libtorch 构建程序时出现链接问题

Linkage problem when building program with libtorch on Raspberry PI

我正在使用 libtorch 和 OpenCV 编写 C++ 程序。

这是我的 CMakeLists.txt 的输出,库版本:

-- 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

您可以看到手电筒需要 libc10.so 才能工作。

当我调用 make 时,一切正常,直到在链接期间发生这种情况:

[  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

当我对该文件执行 file 命令时,我看到它是为 x86-64 架构编译的,而不是 ARM。 这是 /usr/lib/libc10.so/usr/lib/libtorch.so.3.0.0file 命令的输出以进行比较:

/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

我怎样才能有一个工作的 libc10.so 文件?

编辑:

我开始在我的 raspberry PI 上重新构建 PyTorch,因此它也会重新构建 libtorch。 这需要很多小时,并且在夜间因连接问题而崩溃:

/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

但是,构建输出了一个具有以下签名的 libc10.so 文件!

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

我将它替换到 /usr/lib 文件夹中并尝试构建我的项目:它比以前更进一步,并因另一个文件格式问题而崩溃。这证明,如果我设法完全重建 libtorch,我应该能够构建我的项目!

谁能帮我解决新的链接问题?

编辑#2:

我发现 this issue 有同样的问题。尝试给定的解决方案,如果可行,请返回此处编写完整的教程。

我发现 link 可以解决您面临的问题。请仔细阅读它应该解决你的问题。 https://github.com/pytorch/pytorch/issues/14727

我终于成功了,并在 的答案中进行了详细说明。

如果您发现错误/不良做法,请毫不犹豫地改进它!