Skip to content

Commit 6757c6e

Browse files
committed
Improve static linking error/documentation (#174)
1 parent 74e4c3a commit 6757c6e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,31 @@ The availability of `llvm-config` is not optional for static linking. Ensure tha
8686

8787
**Note:** The `libcpp` Cargo feature can be used to enable linking to `libc++` instead of `libstd++` when linking to `libclang` statically on Linux or Haiku.
8888

89+
#### Static Library Availability
90+
91+
Linking to `libclang` statically on *nix systems requires that the `libclang.a` static library be available.
92+
This library is usually *not* included in most distributions of LLVM and Clang (e.g., `libclang-dev` on Debian-based systems).
93+
If you need to link to `libclang` statically then most likely the only consistent way to get your hands on `libclang.a` is to build it yourself.
94+
95+
Here's an example of building the required static libraries and using them with `clang-sys`:
96+
97+
```text
98+
git clone [email protected]:llvm/llvm-project.git
99+
cd llvm-project
100+
101+
cmake -S llvm -B build -G Ninja -DLLVM_ENABLE_PROJECTS=clang -DLIBCLANG_BUILD_STATIC=ON
102+
ninja -C build
103+
104+
cd ..
105+
git clone [email protected]:KyleMayes/clang-sys.git
106+
cd clang-sys
107+
108+
LLVM_CONFIG_PATH=../llvm-project/build/bin/llvm-config cargo test --features static
109+
```
110+
111+
Linking to `libclang` statically requires linking a large number of big static libraries.
112+
Using [`rust-lld` as a linker](https://blog.rust-lang.org/2024/05/17/enabling-rust-lld-on-linux.html) can greatly reduce linking times.
113+
89114
### Runtime
90115

91116
The `clang_sys::load` function is used to load a `libclang` shared library for use in the thread in which it is called. The `clang_sys::unload` function will unload the `libclang` shared library. `clang_sys::load` searches for a `libclang` shared library in the same way one is searched for when linking to `libclang` dynamically at compiletime.

build/static.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ fn find() -> PathBuf {
8787
if let Some((directory, _)) = files.into_iter().next() {
8888
directory
8989
} else {
90-
panic!("could not find any static libraries");
90+
panic!(
91+
"could not find the required `{name}` static library, see the \
92+
README for more information on how to link to `libclang` statically: \
93+
https://github.com/KyleMayes/clang-sys?tab=readme-ov-file#static"
94+
);
9195
}
9296
}
9397

0 commit comments

Comments
 (0)