KyleMayes / clang-sys

Rust bindings for libclang.
Apache License 2.0
128 stars 65 forks source link

Cannot find libclang #140

Closed Jesse-Millwood closed 2 years ago

Jesse-Millwood commented 2 years ago

When buiding a project with cargo-sys as a dependency, It fails to compile the crate, reporting it cannot find the libclang library.

I have a very minimal project with a Cargo.toml:

[package]
name = "clang-sys-test"
version = "0.1.0"
edition = "2021"

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

[dependencies]
clang-sys = "1.3.0

I have tried building like this:

-> export LLVM_CONFIG_PATH=$(which llvm-config)
-> export LIBCLANG_PATH=$(llvm-config --libdir)
-> export RUST_BACKTRACE=1

-> cargo build

    Updating crates.io index
   Compiling clang-sys v1.3.0
error: failed to run custom build command for `clang-sys v1.3.0`

Caused by:
  process didn't exit successfully: `/home/jesse/woollylabs/code/gitrepos/clang-sys-test/target/debug/build/clang-sys-e47d50ee70f29549/build-script-build` (exit status: 101)
  --- stderr
  thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: "couldn't find any valid shared libraries matching: ['libclang.so', 'libclang-*.so'], set the `LIBCLANG_PATH` environment variable to a path where one of these files can be found (invalid: [])"', /home/jesse/.cargo/registry/src/github.com-1ecc6299db9ec823/clang-sys-1.3.0/build/dynamic.rs:212:45
  stack backtrace:
     0: rust_begin_unwind
               at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/std/src/panicking.rs:498:5
     1: core::panicking::panic_fmt
               at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/panicking.rs:107:14
     2: core::result::unwrap_failed
               at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/result.rs:1613:5
     3: core::result::Result<T,E>::unwrap
               at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/result.rs:1295:23
     4: build_script_build::dynamic::link
               at ./build/dynamic.rs:212:33
     5: build_script_build::main
               at ./build.rs:78:9
     6: core::ops::function::FnOnce::call_once
               at /rustc/db9d1b20bba1968c1ec1fc49616d4742c1725b4b/library/core/src/ops/function.rs:227:5
  note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Where which llvm-config returns /usr/bin/llvm-config and llvm-config --libdir returns /usr/lib/llvm-13/lib, and there is the following:

My operating system is Pop!_OS 21.10. My rustc version is 1.58.1. I installed libclang-dev from apt.

Am I missing something here? Does anyone have this issue? I ran into this issue when trying to build opencv-rust and have recreated it in an even more minimal project.

KyleMayes commented 2 years ago

It looks like the issue might be the filenames of the shared libraries.

Because you haven't enabled any clang-sys features, it will by default attempt to link to the shared libraries dynamically (rather than loading them at runtime with the runtime feature or linking them statically with the static feature).

This means it will look for shared libraries with the filename patterns mentioned in the error message: ['libclang.so', 'libclang-*.so'].

None of the shared libraries you listed match these patterns so they are not being selected.

I have found that these shared libraries with the trailing version numbers cannot be linked to by ld so they are not included as linking candidates except when the runtime feature is enabled (because then the filename doesn't matter).

You'll either need to do runtime linking or create symlinks with an ld-compatible name like libclang-13.so. This might be possible by installing a different package, possibly something like libclang-dev. (not familiar with Pop!_OS).

There is room for improvement in clang-sys here though. Perhaps the library finding code could recognize that it has encountered shared libraries with these unusable filenames and make the above suggestion (runtime feature or symlink).

Jesse-Millwood commented 2 years ago

Hey @KyleMayes , thanks for the time you put into the response. I'm an idiot. I installed llvm-dev and not libclang-dev.

Jesse-Millwood commented 2 years ago

For what it's worth, it does look like the libclang.so points to a libclang.so.1:

─➤  ls /usr/lib/llvm-13/lib/libclang.* -la
lrwxrwxrwx 1 root root 39 Oct  3 16:38 /usr/lib/llvm-13/lib/libclang.so -> ../../x86_64-linux-gnu/libclang-13.so.1
lrwxrwxrwx 1 root root 16 Oct  3 16:38 /usr/lib/llvm-13/lib/libclang.so.1 -> libclang-13.so.1