opencv-rust: a `libclang` shared library is not loaded on this thread

Before reporting an issue please first check the

  1. Operating system: Fedora 38
  2. The way you installed OpenCV: package
  3. OpenCV version: opencv-devel-4.7.0-2.fc38.x86_64
  4. rustc version: rustc 1.69.0 (84c898d65 2023-04-16)
  5. Attach the full output of the following command from your project directory:
    RUST_BACKTRACE=full cargo build -vv 
    

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 35 (15 by maintainers)

Most upvoted comments

Since version 0.86.1 you should be getting a more friendly message when libclang loading mismatch is detected: https://github.com/twistedfall/opencv-rust/blob/730cc64955ab4ad5286bee891e795a1322b053c0/build/generator.rs#L74-L77. Thanks for the suggestion!

I can see the problem now. The general idea is that all crates using clang-sys must either all enable its runtime feature or all not. It won’t work if some are enabling it and some are not (at least with rust edition before 2021 with resolver before 2).

In your case there are 3 dependencies that are using clang-sys: opencv, v4l and libspa. The latter two are using it via bindgen. The bindgen crate enables the runtime feature of clang-sys by default, but allows opting out of it.

The problem is that v4l uses bindgen with default features (=runtime enabled), but libspa uses it with default features disabled (=runtime disabled). Neither of those allow you to control it (with opencv you can optionally enable runtime by using clang-runtime feature).

So long story short, your build will fail even without opencv and to solve it you should either enabled the default features with bindgen in libspa-sys or disable them in v4l2-sys and adjust the clang-runtime feature for opencv accordingly. That can be done by either creating issues or PRs in the corresponding repositories or forking the repos and doing the necessary changes yourself.

@zhouzq-thu The same solution should also work for you: make sure to use version 0.81.5 and enable the new clang-runtime feature of the opencv crate.

0.81.5 works with “clang-runtime” feature enabled on macOS. Thanks.

@jaques-sam Ah, wait, I’ve missed the fact that you’re using OpenCV 3.2, that version is no longer supported so you should probably stick with whatever crate version works for you until you are able to switch to OpenCV 3.4 or 4.x

0.81.5 works with “clang-runtime” feature enabled, thanks

@twistedfall @hsyan2008 I found that it is OK if no other crates in the project, but failed if some crates in it.

Try this:

opencv-compile-test.zip

With 0.81.4,

export DYLD_FALLBACK_LIBRARY_PATH="$(xcode-select --print-path)/Toolchains/XcodeDefault.xctoolchain/usr/lib/" && cargo build --all --target=x86_64-apple-darwin

generates:

  === Detected OpenCV module header dir at: /usr/local/opt/opencv/include/opencv4/opencv2
  === Found OpenCV version: 4.7.0 in headers located at: /usr/local/opt/opencv/include/opencv4
  === Generating code in: /Users/zhou/Desktop/rust/opencv-compile-test/target/x86_64-apple-darwin/debug/build/opencv-c8232f0434341892/out
  === Placing generated bindings into: /Users/zhou/Desktop/rust/opencv-compile-test/target/x86_64-apple-darwin/debug/build/opencv-c8232f0434341892/out/opencv
  === Using OpenCV headers from: /usr/local/opt/opencv/include/opencv4
  thread 'main' panicked at 'a `libclang` shared library is not loaded on this thread', /Users/zhou/.cargo/registry/src/github.com-1ecc6299db9ec823/clang-sys-1.6.1/src/lib.rs:1735:1
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

0.80.0 can be built correctly.

@zhouzq-thu

In my Cargo.toml like this

log = "0.4.17"
tokio = { version = "1.26", features = ["full"]  }
anyhow = "1.0.70"
thirtyfour = { version = "0.31.0" }
toml = "0.7.3"
serde = "1.0.160"
tokio-cron-scheduler = { version = "0.9.4", features = ["signal"] }
rbatis = { version = "3.1", default-features = false, features = ["runtime-tokio-rustls","mysql"]}
rbson = { version = "2.0", features = ["uuid-0_8", "chrono-0_4"] }
futures = "0.3.28"
serde_json = "1.0.96"
clap = "4.2.4"

axum = "0.5.6"
which = "4.4.0"
chromiumoxide = "0.5.0"
base64 = "0.21.0"
serde_repr = "0.1.12"
opencv = "0.81.4"

and build by

#opencv library in /usr/local/lib/
export DYLD_FALLBACK_LIBRARY_PATH="/usr/local/lib/:/Library/Developer/CommandLineTools/usr/lib/"
cargo build --target=x86_64-apple-darwin
cargo build --release --target x86_64-unknown-linux-musl

all above is success

Can you try using

cargo build --release --target x86_64-unknown-linux-musl