Rust cargo build fails when I depend on r2r and opencv

I am trying to track an object with opencv and publish the center of rectangle to a ROS topic in Rust.

My Cargo.toml looks like this:

[package]
name = "test_package"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
opencv = "0.86.1"
r2r = "0.8.0"

when I cargo build, it fails to build the packages

The error looks like this:

    error: failed to run custom build command for `opencv v0.86.1`
    
    Caused by:
      process didn't exit successfully: `/home/light/test_package/target/debug/build/opencv-27479df15cc97d32/build-script-build` (exit status: 101)
      --- stdout
      cargo:rerun-if-env-changed=OPENCV4_NO_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=OPENCV4_STATIC
      cargo:rerun-if-env-changed=OPENCV4_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=SYSROOT
      cargo:rerun-if-env-changed=OPENCV4_STATIC
      cargo:rerun-if-env-changed=OPENCV4_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG
      cargo:rerun-if-env-changed=PKG_CONFIG
      cargo:rerun-if-env-changed=OPENCV4_STATIC
      cargo:rerun-if-env-changed=OPENCV4_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
      cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
      cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
      cargo:rustc-cfg=ocvrs_opencv_branch_4
      cargo:rerun-if-env-changed=OPENCV_PACKAGE_NAME
      cargo:rerun-if-env-changed=OPENCV_PKGCONFIG_NAME
      cargo:rerun-if-env-changed=OPENCV_CMAKE_NAME
      cargo:rerun-if-env-changed=OPENCV_CMAKE_BIN
      cargo:rerun-if-env-changed=OPENCV_VCPKG_NAME
      cargo:rerun-if-env-changed=OPENCV_LINK_LIBS
      cargo:rerun-if-env-changed=OPENCV_LINK_PATHS
      cargo:rerun-if-env-changed=OPENCV_INCLUDE_PATHS
      cargo:rerun-if-env-changed=OPENCV_DISABLE_PROBES
      cargo:rerun-if-env-changed=OPENCV_MSVC_CRT
      cargo:rerun-if-env-changed=CMAKE_PREFIX_PATH
      cargo:rerun-if-env-changed=OpenCV_DIR
      cargo:rerun-if-env-changed=PKG_CONFIG_PATH
      cargo:rerun-if-env-changed=VCPKG_ROOT
      cargo:rerun-if-env-changed=VCPKGRS_DYNAMIC
      cargo:rerun-if-env-changed=VCPKGRS_TRIPLET
      cargo:rerun-if-env-changed=OCVRS_DOCS_GENERATE_DIR
      cargo:rerun-if-env-changed=DOCS_RS
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/alphamat.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/aruco.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/bioinspired.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/ccalib.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/core.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/dnn.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/face.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/gapi.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/hdf.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/manual-core.cpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/ocvrs_common.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/photo.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/sfm.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/videoio.hpp
      cargo:rerun-if-changed=/home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.86.1/src_cpp/xfeatures2d.hpp
      cargo:rerun-if-changed=Cargo.toml
    
      --- stderr
      === Crate version: Some("0.86.1")
      === Environment configuration:
      ===   OPENCV_PACKAGE_NAME = None
      ===   OPENCV_PKGCONFIG_NAME = None
      ===   OPENCV_CMAKE_NAME = None
      ===   OPENCV_CMAKE_BIN = None
      ===   OPENCV_VCPKG_NAME = None
      ===   OPENCV_LINK_LIBS = None
      ===   OPENCV_LINK_PATHS = None
      ===   OPENCV_INCLUDE_PATHS = None
      ===   OPENCV_DISABLE_PROBES = None
      ===   OPENCV_MSVC_CRT = None
      ===   CMAKE_PREFIX_PATH = None
      ===   OpenCV_DIR = None
      ===   PKG_CONFIG_PATH = None
      ===   VCPKG_ROOT = None
      ===   VCPKGRS_DYNAMIC = None
      ===   VCPKGRS_TRIPLET = None
      ===   OCVRS_DOCS_GENERATE_DIR = None
      ===   DOCS_RS = None
      ===   PATH = Some("/opt/ros/humble/bin:/home/light/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin")
      === Enabled features:
      ===   ALPHAMAT
      ===   ARUCO
      ===   ARUCO_DETECTOR
      ===   BARCODE
      ===   BGSEGM
      ===   BIOINSPIRED
      ===   CALIB3D
      ===   CCALIB
      ===   CUDAARITHM
      ===   CUDABGSEGM
      ===   CUDACODEC
      ===   CUDAFEATURES2D
      ===   CUDAFILTERS
      ===   CUDAIMGPROC
      ===   CUDAOBJDETECT
      ===   CUDAOPTFLOW
      ===   CUDASTEREO
      ===   CUDAWARPING
      ===   CVV
      ===   DEFAULT
      ===   DNN
      ===   DNN_SUPERRES
      ===   DPM
      ===   FACE
      ===   FEATURES2D
      ===   FLANN
      ===   FREETYPE
      ===   FUZZY
      ===   GAPI
      ===   HDF
      ===   HFS
      ===   HIGHGUI
      ===   IMGCODECS
      ===   IMGPROC
      ===   IMG_HASH
      ===   INTENSITY_TRANSFORM
      ===   LINE_DESCRIPTOR
      ===   MCC
      ===   ML
      ===   OBJDETECT
      ===   OPTFLOW
      ===   OVIS
      ===   PHASE_UNWRAPPING
      ===   PHOTO
      ===   PLOT
      ===   QUALITY
      ===   RAPID
      ===   RGBD
      ===   SALIENCY
      ===   SFM
      ===   SHAPE
      ===   STEREO
      ===   STITCHING
      ===   STRUCTURED_LIGHT
      ===   SUPERRES
      ===   SURFACE_MATCHING
      ===   TEXT
      ===   TRACKING
      ===   VIDEO
      ===   VIDEOIO
      ===   VIDEOSTAB
      ===   VIZ
      ===   WECHAT_QRCODE
      ===   XFEATURES2D
      ===   XIMGPROC
      ===   XOBJDETECT
      ===   XPHOTO
      === Detected probe priority based on environment vars: pkg_config: false, cmake: false, vcpkg: false
      === Probing the OpenCV library in the following order: environment, pkg_config, cmake, vcpkg_cmake, vcpkg
      === Can't probe using: environment, continuing with other methods because: Some environment variables are missing
      === Probing OpenCV library using pkg_config
      === Successfully probed using: pkg_config
      === OpenCV library configuration: Library {
          include_paths: [
              "/usr/include/opencv4",
          ],
          version: Version {
              major: 4,
              minor: 5,
              patch: 4,
          },
          cargo_metadata: [
              "cargo:rustc-link-search=/usr/lib/x86_64-linux-gnu",
              "cargo:rustc-link-lib=opencv_stitching",
              "cargo:rustc-link-lib=opencv_alphamat",
              "cargo:rustc-link-lib=opencv_aruco",
              "cargo:rustc-link-lib=opencv_barcode",
              "cargo:rustc-link-lib=opencv_bgsegm",
              "cargo:rustc-link-lib=opencv_bioinspired",
              "cargo:rustc-link-lib=opencv_ccalib",
              "cargo:rustc-link-lib=opencv_dnn_objdetect",
              "cargo:rustc-link-lib=opencv_dnn_superres",
              "cargo:rustc-link-lib=opencv_dpm",
              "cargo:rustc-link-lib=opencv_face",
              "cargo:rustc-link-lib=opencv_freetype",
              "cargo:rustc-link-lib=opencv_fuzzy",
              "cargo:rustc-link-lib=opencv_hdf",
              "cargo:rustc-link-lib=opencv_hfs",
              "cargo:rustc-link-lib=opencv_img_hash",
              "cargo:rustc-link-lib=opencv_intensity_transform",
              "cargo:rustc-link-lib=opencv_line_descriptor",
              "cargo:rustc-link-lib=opencv_mcc",
              "cargo:rustc-link-lib=opencv_quality",
              "cargo:rustc-link-lib=opencv_rapid",
              "cargo:rustc-link-lib=opencv_reg",
              "cargo:rustc-link-lib=opencv_rgbd",
              "cargo:rustc-link-lib=opencv_saliency",
              "cargo:rustc-link-lib=opencv_shape",
              "cargo:rustc-link-lib=opencv_stereo",
              "cargo:rustc-link-lib=opencv_structured_light",
              "cargo:rustc-link-lib=opencv_phase_unwrapping",
              "cargo:rustc-link-lib=opencv_superres",
              "cargo:rustc-link-lib=opencv_optflow",
              "cargo:rustc-link-lib=opencv_surface_matching",
              "cargo:rustc-link-lib=opencv_tracking",
              "cargo:rustc-link-lib=opencv_highgui",
              "cargo:rustc-link-lib=opencv_datasets",
              "cargo:rustc-link-lib=opencv_text",
              "cargo:rustc-link-lib=opencv_plot",
              "cargo:rustc-link-lib=opencv_ml",
              "cargo:rustc-link-lib=opencv_videostab",
              "cargo:rustc-link-lib=opencv_videoio",
              "cargo:rustc-link-lib=opencv_viz",
              "cargo:rustc-link-lib=opencv_wechat_qrcode",
              "cargo:rustc-link-lib=opencv_ximgproc",
              "cargo:rustc-link-lib=opencv_video",
              "cargo:rustc-link-lib=opencv_xobjdetect",
              "cargo:rustc-link-lib=opencv_objdetect",
              "cargo:rustc-link-lib=opencv_calib3d",
              "cargo:rustc-link-lib=opencv_imgcodecs",
              "cargo:rustc-link-lib=opencv_features2d",
              "cargo:rustc-link-lib=opencv_dnn",
              "cargo:rustc-link-lib=opencv_flann",
              "cargo:rustc-link-lib=opencv_xphoto",
              "cargo:rustc-link-lib=opencv_photo",
              "cargo:rustc-link-lib=opencv_imgproc",
              "cargo:rustc-link-lib=opencv_core",
          ],
      }
      === Detected OpenCV module header dir at: /usr/include/opencv4/opencv2
      === Found OpenCV version: 4.5.4 in headers located at: /usr/include/opencv4
      === Generating code in: /home/light/test_package/target/debug/build/opencv-56dc7eb1f2d57af6/out
      === Placing generated bindings into: /home/light/test_package/target/debug/build/opencv-56dc7eb1f2d57af6/out/opencv
      === Using OpenCV headers from: /usr/include/opencv4
      thread 'main' panicked at /home/light/.cargo/registry/src/index.crates.io-6f17d22bba15001f/clang-sys-1.6.1/src/lib.rs:1735:1:
      a `libclang` shared library is not loaded on this thread
      note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    warning: build failed, waiting for other jobs to finish...
  

I am running ubuntu 22.04 LTS with ROS 2 humble.

To reproduce this run these commands :

$cargo new test_packate

$cd test_package

$cargo add r2r

$cargo add opencv

$cargo build

What is the problem here?

I first didn’t source ROS, then r2r does not build. After sourcing ROS, opencv does not build.
opencv and r2r build properly when I don’t depend on both of them. When I depend on only one of them separately it works.

Leave a Comment