Environment
- OS: macOS Sonoma 24.5.0 (Apple Silicon, aarch64-apple-darwin)
- rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
- OpenCV: 4.11.0 (Installed via Homebrew)
- LLVM: 19.1.0 (Installed via Homebrew)
- Xcode Command Line Tools: Installed and up-to-date
Problem Description
When building a project with opencv-rs as a dependency on an Apple Silicon Mac, the build consistently fails during the opencv crate's build script execution.
The error message is:
thread 'main' panicked at /Users/yangqijun/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/clang-sys-1.8.1/src/lib.rs:1859:1: a 'libclang' shared library is not loaded on this thread
This happens even though pkg-config successfully finds the OpenCV library, and all relevant environment variables seem to be set correctly. The issue appears to be with clang-sys's ability to dynamically load libclang.dylib at runtime within the build script.
Cargo.toml Dependency
[dependencies]
opencv = "0.92"
Attempts to fix
We have tried multiple combinations of environment variables, including all standard and community-suggested solutions, but the error persists.
Attempt 1: Basic Paths
export LIBCLANG_PATH="/opt/homebrew/opt/llvm/lib"
export PKG_CONFIG_PATH="/opt/homebrew/opt/opencv/lib/pkgconfig"
cargo build
Result: Failed with the same error.
Attempt 2: Comprehensive Standard Paths
export PKG_CONFIG_PATH="/opt/homebrew/opt/opencv/lib/pkgconfig"
export LIBCLANG_PATH="/opt/homebrew/opt/llvm/lib"
export DYLD_FALLBACK_LIBRARY_PATH="$(xcode-select -p)/Toolchains/XcodeDefault.xctoolchain/usr/lib"
export LD_LIBRARY_PATH="/opt/homebrew/lib"
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
cargo build
Result: Failed with the same error.
Attempt 3: Adding BINDGEN_EXTRA_CLANG_ARGS (based on community suggestions)
export PKG_CONFIG_PATH="/opt/homebrew/opt/opencv/lib/pkgconfig"
export LIBCLANG_PATH="/opt/homebrew/opt/llvm/lib"
export LD_LIBRARY_PATH="/opt/homebrew/lib"
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"
export BINDGEN_EXTRA_CLANG_ARGS="-isysroot $(xcrun --sdk macosx --show-sdk-path)"
cargo build
Result: Still failed with the exact same error.
Final Build Log
This is the output from the last attempt (Attempt 3).
=== Crate version: Some("0.92.3")
=== Environment configuration:
=== OpenCV_DIR = Some("/opt/homebrew/opt/opencv")
=== PKG_CONFIG_PATH = Some("/opt/homebrew/opt/opencv/lib/pkgconfig")
...
=== Probing OpenCV library using pkg_config
=== Successfully probed using: pkg_config
=== OpenCV library configuration: Library {
include_paths: [
"/opt/homebrew/opt/opencv/include/opencv4",
],
version: Version {
major: 4,
minor: 11,
patch: 0,
},
...
}
=== Detected OpenCV module header dir at: /opt/homebrew/opt/opencv/include/opencv4/opencv2
=== Found OpenCV version: 4.11.0 in headers located at: /opt/homebrew/opt/opencv/include/opencv4
=== Generating code in: /path/to/project/src-tauri/target/debug/build/opencv-621a83dcc0ce5751/out
=== Placing generated bindings into: /path/to/project/src-tauri/target/debug/build/opencv-621a83dcc0ce5751/out/opencv
=== Using OpenCV headers from: /opt/homebrew/opt/opencv/include/opencv4
thread 'main' panicked at /Users/yangqijun/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/clang-sys-1.8.1/src/lib.rs:1859:1:
a `libclang` shared library is not loaded on this thread
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Question
Given that all standard environment variable configurations have been attempted, could this be a deeper issue related to System Integrity Protection (SIP), the dynamic linker (dyld), or a specific incompatibility between clang-sys, the Homebrew-installed llvm, and the Apple Silicon architecture?
Are there any other diagnostic steps or environment configurations we can try to resolve this dynamic loading failure?
Thank you for your help.
Environment
Problem Description
When building a project with
opencv-rsas a dependency on an Apple Silicon Mac, the build consistently fails during theopencvcrate's build script execution.The error message is:
thread 'main' panicked at /Users/yangqijun/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/clang-sys-1.8.1/src/lib.rs:1859:1: a 'libclang' shared library is not loaded on this threadThis happens even though
pkg-configsuccessfully finds the OpenCV library, and all relevant environment variables seem to be set correctly. The issue appears to be withclang-sys's ability to dynamically loadlibclang.dylibat runtime within the build script.Cargo.toml Dependency
Attempts to fix
We have tried multiple combinations of environment variables, including all standard and community-suggested solutions, but the error persists.
Attempt 1: Basic Paths
Result: Failed with the same error.
Attempt 2: Comprehensive Standard Paths
Result: Failed with the same error.
Attempt 3: Adding
BINDGEN_EXTRA_CLANG_ARGS(based on community suggestions)Result: Still failed with the exact same error.
Final Build Log
This is the output from the last attempt (Attempt 3).
Question
Given that all standard environment variable configurations have been attempted, could this be a deeper issue related to System Integrity Protection (SIP), the dynamic linker (
dyld), or a specific incompatibility betweenclang-sys, the Homebrew-installedllvm, and the Apple Silicon architecture?Are there any other diagnostic steps or environment configurations we can try to resolve this dynamic loading failure?
Thank you for your help.