opencv-rust icon indicating copy to clipboard operation
opencv-rust copied to clipboard

Cross compiling to arm linux platform : Segmentation fault (core dumped)

Open kingzcheung opened this issue 3 years ago • 6 comments

I am doing cross compiling to arm linux platform. I don't need these modules like viz, sfm,freetype etc. But now I have to add these so libraries to compile.

image image

My dependencies:

[dependencies]
opencv = {version="0.63", default-features = false, features = [ "imgcodecs","videoio","highgui"]}

kingzcheung avatar Apr 24 '22 09:04 kingzcheung

I had specified OPENCV_LINK_LIBS, but i got errors.

[opencv 0.63.1]     stderr: "CMake Error: CMake was unable to find a build program corresponding to \"Ninja\".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.\nCMake Error: CMAKE_C_COMPILER not set, after EnableLanguage\nCMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage\n"
[opencv 0.63.1] === cmake makefiles probe command: "/opt/homebrew/bin/cmake" "-S" "/Users/kingzcheung/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/opencv-0.63.1/cmake" "-DOCVRS_PACKAGE_NAME=OpenCV" "-DCMAKE_BUILD_TYPE=Debug" "-G" "Unix Makefiles"
[opencv 0.63.1] === Probing with cmake Makefile generator failed, will try deprecated find_package, error: cmake returned an error
[opencv 0.63.1]     stdout: "-- The C compiler identification is AppleClang 13.1.6.13160021\n-- The CXX compiler identification is GNU 11.2.0\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Checking whether CXX compiler has -isysroot\n-- Checking whether CXX compiler has -isysroot - yes\n-- Checking whether CXX compiler supports OSX deployment target flag\n-- Checking whether CXX compiler supports OSX deployment target flag - no\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - failed\n-- Check for working CXX compiler: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++\n-- Check for working CXX compiler: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++ - broken\n-- Configuring incomplete, errors occurred!\nSee also \"/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeOutput.log\".\nSee also \"/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeError.log\".\n"
[opencv 0.63.1]     stderr: "CMake Error at /Applications/CMake.app/Contents/share/cmake-3.23/Modules/CMakeTestCXXCompiler.cmake:62 (message):\n  The C++ compiler\n\n    \"/Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++\"\n\n  is not able to compile a simple test program.\n\n  It fails with the following output:\n\n    Change Dir: /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp\n    \n    Run Build Command(s):/opt/homebrew/bin/gmake -f Makefile cmTC_43728/fast && /opt/homebrew/bin/gmake  -f CMakeFiles/cmTC_43728.dir/build.make CMakeFiles/cmTC_43728.dir/build\n    gmake[1]: Entering directory '/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp'\n    Building CXX object CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o\n    /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++   -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -o CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o -c /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx\n    armv7-unknown-linux-musleabihf-g++: error: unrecognized command-line option '-arch'; did you mean '-march='?\n    gmake[1]: *** [CMakeFiles/cmTC_43728.dir/build.make:78: CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o] Error 1\n    gmake[1]: Leaving directory '/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp'\n    gmake: *** [Makefile:127: cmTC_43728/fast] Error 2\n    \n    \n\n  \n\n  CMake will not be able to correctly generate this project.\nCall Stack (most recent call first):\n  CMakeLists.txt:1 (project)\n\n\n"
= note: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/../lib/gcc/armv7-unknown-linux-musleabihf/11.2.0/../../../../armv7-unknown-linux-musleabihf/bin/ld.bfd: /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/deps/libopencv-547e407c497e4921.rlib(videoio.o): in function `cv_VideoCapture_VideoCapture_const_StringR_int':
          /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/videoio.cpp:87: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
          collect2: error: ld returned 1 exit status
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `record` (bin "record") generated 2 warnings
error: could not compile `record` due to previous error; 2 warnings emitted

build1.log

kingzcheung avatar Apr 25 '22 04:04 kingzcheung

I had specified OPENCV_LINK_LIBS, but i got errors.

[opencv 0.63.1]     stderr: "CMake Error: CMake was unable to find a build program corresponding to \"Ninja\".  CMAKE_MAKE_PROGRAM is not set.  You probably need to select a different build tool.\nCMake Error: CMAKE_C_COMPILER not set, after EnableLanguage\nCMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage\n"
[opencv 0.63.1] === cmake makefiles probe command: "/opt/homebrew/bin/cmake" "-S" "/Users/kingzcheung/.cargo/registry/src/mirrors.ustc.edu.cn-61ef6e0cd06fb9b8/opencv-0.63.1/cmake" "-DOCVRS_PACKAGE_NAME=OpenCV" "-DCMAKE_BUILD_TYPE=Debug" "-G" "Unix Makefiles"
[opencv 0.63.1] === Probing with cmake Makefile generator failed, will try deprecated find_package, error: cmake returned an error
[opencv 0.63.1]     stdout: "-- The C compiler identification is AppleClang 13.1.6.13160021\n-- The CXX compiler identification is GNU 11.2.0\n-- Detecting C compiler ABI info\n-- Detecting C compiler ABI info - done\n-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc - skipped\n-- Detecting C compile features\n-- Detecting C compile features - done\n-- Checking whether CXX compiler has -isysroot\n-- Checking whether CXX compiler has -isysroot - yes\n-- Checking whether CXX compiler supports OSX deployment target flag\n-- Checking whether CXX compiler supports OSX deployment target flag - no\n-- Detecting CXX compiler ABI info\n-- Detecting CXX compiler ABI info - failed\n-- Check for working CXX compiler: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++\n-- Check for working CXX compiler: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++ - broken\n-- Configuring incomplete, errors occurred!\nSee also \"/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeOutput.log\".\nSee also \"/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeError.log\".\n"
[opencv 0.63.1]     stderr: "CMake Error at /Applications/CMake.app/Contents/share/cmake-3.23/Modules/CMakeTestCXXCompiler.cmake:62 (message):\n  The C++ compiler\n\n    \"/Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++\"\n\n  is not able to compile a simple test program.\n\n  It fails with the following output:\n\n    Change Dir: /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp\n    \n    Run Build Command(s):/opt/homebrew/bin/gmake -f Makefile cmTC_43728/fast && /opt/homebrew/bin/gmake  -f CMakeFiles/cmTC_43728.dir/build.make CMakeFiles/cmTC_43728.dir/build\n    gmake[1]: Entering directory '/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp'\n    Building CXX object CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o\n    /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++   -arch arm64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk -o CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o -c /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx\n    armv7-unknown-linux-musleabihf-g++: error: unrecognized command-line option '-arch'; did you mean '-march='?\n    gmake[1]: *** [CMakeFiles/cmTC_43728.dir/build.make:78: CMakeFiles/cmTC_43728.dir/testCXXCompiler.cxx.o] Error 1\n    gmake[1]: Leaving directory '/Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/cmake_probe_build/CMakeFiles/CMakeTmp'\n    gmake: *** [Makefile:127: cmTC_43728/fast] Error 2\n    \n    \n\n  \n\n  CMake will not be able to correctly generate this project.\nCall Stack (most recent call first):\n  CMakeLists.txt:1 (project)\n\n\n"
= note: /Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/../lib/gcc/armv7-unknown-linux-musleabihf/11.2.0/../../../../armv7-unknown-linux-musleabihf/bin/ld.bfd: /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/deps/libopencv-547e407c497e4921.rlib(videoio.o): in function `cv_VideoCapture_VideoCapture_const_StringR_int':
          /Users/kingzcheung/rust/videotools/target/armv7-unknown-linux-musleabihf/debug/build/opencv-2aee243441cacc30/out/videoio.cpp:87: undefined reference to `cv::VideoCapture::VideoCapture(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
          collect2: error: ld returned 1 exit status
          
  = help: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

warning: `record` (bin "record") generated 2 warnings
error: could not compile `record` due to previous error; 2 warnings emitted

build1.log

my solution:

export OPENCV_LINK_LIBS=/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_core.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_video.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_highgui.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_gapi.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_imgcodecs.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_imgproc.4.5.5.so,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_videoio.4.5.5.so

I have cross-compiled it, but I can't run it on the corresponding platform.

[root@RV1126_RV1109:/userdata]# ./record
/bin/sh: ./record: not found

kingzcheung avatar Apr 25 '22 07:04 kingzcheung

x86_64-unknown-linux-musl error:

226475 segmentation fault (core dumped)

armv7l-linux-musleabihf-gcc error:

/bin/sh: ./record: not found

It's ok on native(macbook m1,linux x86_64).

kingzcheung avatar Apr 26 '22 01:04 kingzcheung

This is my latest attempt (cross-compile arm mac(m1) => arm linux)

main.rs

use opencv::{
	prelude::*,
};

fn main() {
    println!("Hello, world!");
}

env:

CXX=/Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-g++
OPENCV_LIB_PREFIX=/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib
OPENCV_LINK_LIBS=/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_core.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_video.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_highgui.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_gapi.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_imgcodecs.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_imgproc.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_videoio.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_photo.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_flann.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_features2d.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_stitching.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_shape.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_objdetect.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_superres.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_ml.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_dnn.a,/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib/libopencv_videostab.a
OpenCV_DIR=/Users/kingzcheung/Downloads/opencv-4.5.5
OPENCV_CMAKE_BIN=/opt/homebrew/bin/cmake
CMAKE_MAKE_PROGRAM=/usr/bin/make
OPENCV_INCLUDE_PATHS=/Users/kingzcheung/Downloads/opencv-4.5.5/modules/core/include,/Users/kingzcheung/Downloads/opencv-4.5.5/build
OPENCV_LINK_PATHS=/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib

building cmd:

cargo build -vvv --target=armv7-unknown-linux-musleabihf

config:

[target.armv7-unknown-linux-musleabihf]
linker = "/Users/kingzcheung/armv7-unknown-linux-musleabihf/bin/armv7-unknown-linux-musleabihf-gcc"
rustflags = [
    "-C", "target-feature=+crt-static",
    "-C", "link-arg=-lgcc",
    "-C", "link-arg=-static",
    "-C", "link-arg=-nostartfiles",
    "-L","/Users/kingzcheung/Downloads/opencv-4.5.5/build/lib"
    ]

Run file target:

❯ file ./target/armv7-unknown-linux-musleabihf/debug/demo
./target/armv7-unknown-linux-musleabihf/debug/demo: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /usr/lib/ld.so.1, with debug_info, not stripped

build.log: build.log

error on arm linux:

/bin/sh: ./demo: not found

@twistedfall Thank you for your work and I need some help.

kingzcheung avatar Apr 28 '22 07:04 kingzcheung

Can you check if file /usr/lib/ld.so.1 actually exists in the target system?

twistedfall avatar Apr 28 '22 07:04 twistedfall

Can you check if file /usr/lib/ld.so.1 actually exists in the target system?

I had fixed this error.

[root@RV1126_RV1109:/userdata]# LD_LIBRARY_PATH=/userdata/build/arm-linux-gnueab
ihf/lib:/userdata/build/lib:/usr/lib LD_TRACE_LOADED_OBJECTS=1 ./demo
	linux-vdso.so.1 (0xaeccf000)
	libstdc++.so.6 => /userdata/build/arm-linux-gnueabihf/lib/libstdc++.so.6 (0xa6e61000)
	libopencv_core.so.405 => /userdata/build/lib/libopencv_core.so.405 (0xa6c3d000)
	libopencv_imgproc.so.405 => /userdata/build/lib/libopencv_imgproc.so.405 (0xa696f000)
	libm.so.6 => /userdata/build/arm-linux-gnueabihf/lib/libm.so.6 (0xa6906000)
	libc.so.6 => /userdata/build/arm-linux-gnueabihf/lib/libc.so.6 (0xa6807000)
	/usr/lib/ld.so.1 (0xa6faa000)
	libgcc_s.so.1 => /userdata/build/arm-linux-gnueabihf/lib/libgcc_s.so.1 (0xa67de000)
	libdl.so.2 => /userdata/build/arm-linux-gnueabihf/lib/libdl.so.2 (0xa67cb000)
	libpthread.so.0 => /userdata/build/arm-linux-gnueabihf/lib/libpthread.so.0 (0xa67a5000)

But

main.rs

use opencv::{
	prelude::*,
};

fn main() {
    println!("Hello, world!");
}
[root@RV1126_RV1109:/userdata]# LD_LIBRARY_PATH=/userdata/build/arm-linux-gnueab
ihf/lib:/userdata/build/lib:/usr/lib  ./demo
Segmentation fault (core dumped)

kingzcheung avatar Apr 29 '22 07:04 kingzcheung

I have recently added a sample dockerfile that sets up the crosscompiling container to Raspberry Pi, this might be of use to you too: https://github.com/twistedfall/opencv-rust/blob/master/tools/rpi-xcompile.Dockerfile and the corresponding readme section: https://github.com/twistedfall/opencv-rust#crosscompilation

twistedfall avatar May 12 '23 17:05 twistedfall