twistedfall / opencv-rust

Rust bindings for OpenCV 3 & 4
MIT License
1.96k stars 160 forks source link

unable to build with self-compiled OpenCV (3 or 4) #496

Closed mdhoney closed 11 months ago

mdhoney commented 1 year ago

Machine 1 runs Ubuntu 20.04 and installing OpenCV using apt-get gives me version 4.2. I am specifying opencv = "0.84.4" in my Cargo.toml and my code builds as expected...

use opencv::{prelude::*, core, imgcodecs, photo};

Machine 2 runs Ubuntu 18.04 which by default only installs OpenCV 3.2. So I try and manually build it:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. -D PYTHON_DEFAULT_EXECUTABLE=$(which python3) -D OPENCV_GENERATE_PKGCONFIG=ON
make -j8
sudo make install
sudo ldconfig

Seems to build OK. But my same Rust code no longer builds, I get error messages along the lines of:

pub enum unnamed {
    |     ^^^^^^^^^^^^^^^^ `unnamed` redefined here

with older versions of the crate, with opencv = "0.84.4" or newer I get

use crate::core::{Matx44, MatxTrait};
  |            ^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `crate::manual::core`

instead.
I've tried this wih OpenCV 3.4, 4.2 and 4.8.

Is my OpenCV install incomplete or am not passing some required params to rustc?
Thanks for any pointers.

twistedfall commented 1 year ago

Looks like something is indeed missing in your OpenCV build. Can you please attach the result of cargo build -vv after doing cargo clean?

mdhoney commented 1 year ago

Looks like something is indeed missing in your OpenCV build. Can you please attach the result of cargo build -vv after doing cargo clean?

Thanks for your reply! Here is the output from cargo build -vv: cargo_build.log.gz

mdhoney commented 1 year ago

OK, maybe this is more relevant... I built and installed both 3.4.20 and then 4.2.0 just to have all my bases covered. I then get the following cargo build errors:

  --- stderr
  === Crate version: Some("0.84.5")
  === 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("/home/adapt/.local/bin:/home/adapt/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/cuda/bin:/opt/ImageJ/bin:/opt/arduino-1.8.13/:/home/adapt/.local/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/local/include/opencv4/opencv",
          "/usr/local/include/opencv4",
      ],
      version: Version {
          major: 4,
          minor: 2,
          patch: 0,
      },
      cargo_metadata: [
          "cargo:rustc-link-search=/usr/local/lib",
          "cargo:rustc-link-lib=opencv_dnn",
          "cargo:rustc-link-lib=opencv_gapi",
          "cargo:rustc-link-lib=opencv_highgui",
          "cargo:rustc-link-lib=opencv_ml",
          "cargo:rustc-link-lib=opencv_objdetect",
          "cargo:rustc-link-lib=opencv_photo",
          "cargo:rustc-link-lib=opencv_stitching",
          "cargo:rustc-link-lib=opencv_video",
          "cargo:rustc-link-lib=opencv_calib3d",
          "cargo:rustc-link-lib=opencv_features2d",
          "cargo:rustc-link-lib=opencv_flann",
          "cargo:rustc-link-lib=opencv_videoio",
          "cargo:rustc-link-lib=opencv_imgcodecs",
          "cargo:rustc-link-lib=opencv_imgproc",
          "cargo:rustc-link-lib=opencv_core",
      ],
  }
  === Detected OpenCV module header dir at: /usr/local/include/opencv4/opencv2
  === Found OpenCV version: 4.2.0 in headers located at: /usr/local/include/opencv4
  === Generating code in: /home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out
  === Placing generated bindings into: /home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out/opencv
  === Using OpenCV headers from: /usr/local/include/opencv4
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Clang: clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
  === Clang command line args: [
      "-isystem/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0",
      "-isystem/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0",
      "-isystem/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/backward",
      "-isystem/usr/include/clang/6.0.0/include",
      "-isystem/usr/local/include",
      "-isystem/usr/include/x86_64-linux-gnu",
      "-isystem/usr/include",
      "-I/usr/local/include/opencv4",
      "-F/usr/local/include/opencv4",
      "-I/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp",
      "-F/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp",
      "-DOCVRS_PARSING_HEADERS",
      "-includeocvrs_common.hpp",
      "-std=c++14",
  ]
  === Available jobs from the environment created jobserver is: 0 or there is an error reading that value
  === Creating a new job server with num_jobs: 18
  === Generating 15 modules
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "ml" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "highgui" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "flann" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "photo" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "objdetect" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "videoio" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "features2d" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "stitching" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "core" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "imgproc" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "dnn" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "video" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "imgcodecs" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "calib3d" "/usr/local/include/opencv4/opencv"
  === Running: "/home/adapt/git/adaptgui/target/debug/build/opencv-28b9bde70230b778/build-script-build" "/usr/local/include/opencv4" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "gapi" "/usr/local/include/opencv4/opencv"
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2=== Cannot canonicalize one of the additional_include_dirs: )
  /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Cannot canonicalize one of the additional_include_dirs: /usr/local/include/opencv4/opencv, reason: No such file or directory (os error 2)
  === Generated: imgcodecs in 1.131282549s
  === Generated: highgui in 1.573711737s
  === Generated: photo in 1.76794952s
  === Generated: video in 1.81764084s
  === Generated: videoio in 1.885384987s
  === Generated: flann in 2.020145952s
  === Generated: features2d in 2.373086127s
  === Generated: ml in 2.534406013s
  === Generated: objdetect in 2.58531576s
  === Generated: calib3d in 2.724303453s
  === Generated: dnn in 2.748350468s
  === Generated: gapi in 2.896808815s
  === Generated: imgproc in 3.075722528s
  === Generated: stitching in 3.811265025s
  === Generated: core in 5.231854467s
  === Total binding generation time: 5.241104874s
  === Total binding collection time: 57.47885ms
  === Compiler information: Tool {
      path: "c++",
      cc_wrapper_path: None,
      cc_wrapper_args: [],
      args: [
          "-O3",
          "-ffunction-sections",
          "-fdata-sections",
          "-fPIC",
          "-gdwarf-4",
          "-fno-omit-frame-pointer",
          "-m64",
          "-I",
          "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp",
          "-I",
          "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out",
          "-I",
          ".",
          "-I",
          "/usr/local/include/opencv4/opencv",
          "-I",
          "/usr/local/include/opencv4",
          "-Wall",
          "-Wextra",
          "-std=c++11",
          "-Wno-deprecated-declarations",
          "-Wno-deprecated-copy",
          "-Wno-unused-parameter",
          "-Wno-sign-compare",
          "-Wno-comment",
          "-Wno-unused-variable",
          "-Wno-ignored-qualifiers",
          "-Wno-return-type-c-linkage",
      ],
      env: [],
      family: Gnu,
      cuda: false,
      removed_args: [],
  }

  error occurred: Command "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "-I" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out" "-I" "." "-I" "/usr/local/include/opencv4/opencv" "-I" "/usr/local/include/opencv4" "-Wall" "-Wextra" "-std=c++11" "-Wno-deprecated-declarations" "-Wno-deprecated-copy" "-Wno-unused-parameter" "-Wno-sign-compare" "-Wno-comment" "-Wno-unused-variable" "-Wno-ignored-qualifiers" "-Wno-return-type-c-linkage" "-o" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out/725f15e7c91fc901-gapi.o" "-c" "/home/adapt/git/adaptgui/target/debug/build/opencv-fd5688ed95329c5f/out/gapi.cpp" with args "c++" did not execute successfully (status code exit status: 1).

I noticed it is complaining about a missing /usr/local/include/opencv4/opencv folder. If I copy /usr/local/include/opencv into /usr/local/include/opencv4 I still get a c++ error though:

  error occurred: Command "c++" "-O3" "-ffunction-sections" "-fdata-sections" "-fPIC" "-gdwarf-4" "-fno-omit-frame-pointer" "-m64" "-I" "/home/adapt/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-0.84.5/src_cpp" "-I" "/home/adapt/git/adaptgui/target/debug/build/opencv-f393a43f778b194b/out" "-I" "." "-I" "/usr/local/include/opencv4/opencv" "-I" "/usr/local/include/opencv4" "-Wall" "-Wextra" "-std=c++11" "-Wno-deprecated-declarations" "-Wno-deprecated-copy" "-Wno-unused-parameter" "-Wno-sign-compare" "-Wno-comment" "-Wno-unused-variable" "-Wno-ignored-qualifiers" "-Wno-return-type-c-linkage" "-o" "/home/adapt/git/adaptgui/target/debug/build/opencv-f393a43f778b194b/out/db36d6d3d5e43ec9-gapi.o" "-c" "/home/adapt/git/adaptgui/target/debug/build/opencv-f393a43f778b194b/out/gapi.cpp" with args "c++" did not execute successfully (status code exit status: 1).
twistedfall commented 1 year ago

The errors are basically saying it can't find the core module which shouldn't be possible. Can you please tell me if it's possible to for you to reproduce this issue in a separate project with only the opencv dependency and using the stable rustc compiler?

PlasmaPower commented 11 months ago

I think I'm hitting the same issue. Here's cargo build -vv with the opencv build script output included: https://gist.github.com/PlasmaPower/3bb7b27f00847f86f93e4a467a47b0dd

The root issue for me seems to me:

error: expected identifier, found `(`
  --> project/target/release/build/opencv-628c3dfbb70ae270/out/opencv/bioinspired.rs:21:11
   |
21 |     pub enum (anonymous enum at /usr/local/include/opencv4/opencv2/bioinspired/retina.hpp:83:1) {
   |              ^ expected identifier

That then causes the "unresolved import crate::core" issue, because core couldn't build/parse.

I'm using a locally built opencv 4.8.1 with opencv-contrib 4.8.1. I think this issue started occurring when I updated some dependency of opencv, but I could be wrong there. I also updated rust and some other stuff around the same time.

twistedfall commented 11 months ago

I'm 99% sure it's because of the clang version, 6 is quite old so as a workaround you could try using newer clang, but I'll investigate and try fix this issue in any case.

PlasmaPower commented 11 months ago

Yeah, I'm pretty sure that's right. I modified the is_anonymous check in binding-generator's enumeration.rs to add an additional workaround checking if the enum's name started with '(' and that seemed to fix it. I can PR that if you'd like.

twistedfall commented 11 months ago

Thanks for checking! If the fix looks like this: https://github.com/twistedfall/opencv-rust/commit/0f5a773433c930d08ce71a8615cbab9190cccf9b then no need for a PR, I'm releasing it as I write the comment, but if there is something more to it, I would gladly check it out!

twistedfall commented 11 months ago

The fix should now be available in 0.85.2

PlasmaPower commented 11 months ago

That fixes it for me. Thank you so much!