# Rust + OpenCV = libLLVM-16.so: cannot open shared object file

**URL:** https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524
**Category:** Help
**Created:** [April 30, 2024, 3:20pm UTC](https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524 "2024-04-30T15:20:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![mamaf](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mamaf](https://discourse.nixos.org/u/mamaf)
#### Post date: [April 30, 2024, 3:20pm UTC](https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524/1 "2024-04-30T15:20:43Z")

</div>

I’m trying to use opencv with rust, but run into a strange issue. Rust is installed globally on the system.

Creating a hello-world app:

```bash
cargo init myapp && cd myapp

# adding OpenCV dependency in rust project
cargo add opencv --features=clang-runtime

nix-shell -p pkg-config opencv
cargo check

```

I got next error:

```plaintext
=== Detected OpenCV module header dir at: /nix/store/rlf6jmwp306wr1bv2gi7bhcq7kwn9bhj-opencv-4.9.0/include/opencv4/opencv2
  === Found OpenCV version: 4.9.0 in headers located at: /nix/store/rlf6jmwp306wr1bv2gi7bhcq7kwn9bhj-opencv-4.9.0/include/opencv4
  === Generating code in: /home/amv/Projects/dev/target/debug/build/opencv-c5368590cc5f4abf/out
  === Placing generated bindings into: /home/amv/Projects/dev/target/debug/build/opencv-c5368590cc5f4abf/out/opencv
  === Using OpenCV headers from: /nix/store/rlf6jmwp306wr1bv2gi7bhcq7kwn9bhj-opencv-4.9.0/include/opencv4
  thread 'main' panicked at /home/amv/.cargo/registry/src/index.crates.io-6f17d22bba15001f/opencv-binding-generator-0.89.0/src/generator.rs:387:33:
  Can't initialize clang: "the `libclang` shared library at /usr/lib/libclang.so.16.0.6 could not be opened: libLLVM-16.so: cannot open shared object file: No such file or directory"
  note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

```

At the same time inside `nix-shell` I have:

```bash
> ls /usr/lib|grep libclang
libclang-cpp.so
libclang-cpp.so.16
libclang.so
libclang.so.16
libclang.so.16.0.6

> ls /usr/lib|grep LLVM    
LLVMgold.so
libLLVM-16.0.6.so
libLLVM-16.so
libLLVM.so
libLLVMDemangle.a
libLLVMSupport.a
libLLVMTableGen.a

```

So I don’t understand what’s wrong and how to solve this issue.

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [April 30, 2024, 8:38pm UTC](https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524/2 "2024-04-30T20:38:03Z")

</div>

welcome @mamaf!  
Two observations that stand out to me:

- you’ll want to grab the `llvmPackages.libclang` too
- libclang does not provide pkg-config files upstream so nix doesn’t appear to magically find them. So you need to populate the correct environment variable that points to the libraries

I ran into this before and found a solution here: [How to correctly populate a clang and llvm development environment using nix-shell?](https://discourse.nixos.org/t/how-to-correctly-populate-a-clang-and-llvm-development-environment-using-nix-shell/3864)

I’m curious if you can do this in a one liner. I was able to load `nix-shell -p pkg-config opencv llvmPackages.libclang` but couldn’t figure out how to specify a shellHook.  
That said, I’d recommend you create an ad hoc shell environment as it makes this much simpler. If you are unfamiliar with that, check out the nix.dev guide: [Ad hoc shell environments — nix.dev documentation](https://nix.dev/tutorials/first-steps/ad-hoc-shell-environments)

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [April 30, 2024, 8:42pm UTC](https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524/3 "2024-04-30T20:42:43Z")

</div>

Actually, [Declarative shell environments with shell.nix — nix.dev documentation](https://nix.dev/tutorials/first-steps/declarative-shell) is a better guide for this use case I think.

---

<div class="post-metadata">

### Author: ![efx](https://discourse.nixos.org/user_avatar/discourse.nixos.org/efx/32/7925_2.png) [@efx](https://discourse.nixos.org/u/efx)
#### Post date: [April 30, 2024, 9:08pm UTC](https://discourse.nixos.org/t/rust-opencv-libllvm-16-so-cannot-open-shared-object-file/44524/4 "2024-04-30T21:08:39Z")

</div>

This piqued my curiosity so I took a stab at a shell.nix to do this:

```auto
let
  nixpkgs = fetchTarball "https://github.com/NixOS/nixpkgs/tarball/nixos-23.11";
  pkgs = import nixpkgs {
    config = { };
    overlays = [];
  };
in

pkgs.mkShell {
  packages = with pkgs; [
    opencv
    llvmPackages.libclang.lib
  ];
  nativeBuildInputs = [pkgs.pkg-config];

  shellHook = ''
    export LIBCLANG_PATH="${pkgs.llvmPackages.libclang.lib}/lib";
  '';
}

```

Thanks to other contributors I found the correct syntax (details [LIBCLANG\_PATH environment variable from inside .nix expression · Issue #52447 · NixOS/nixpkgs · GitHub](https://github.com/NixOS/nixpkgs/issues/52447#issuecomment-853429315)).

For me, on NixOS with Rust installed, compilation begins successfully!

```auto
/t/myapp (main)> nix-shell shell.nix

[nix-shell:/tmp/myapp]$ cargo check
   Compiling opencv v0.91.2
    Building [========================>] 35/37: opencv

```

And succeeds 🙂 ` Finished dev [unoptimized + debuginfo] target(s) in 2m 49s`
