2023-09-08 22:25:00 -07:00
|
|
|
# `cbindgen`   [![Build Status]][actions] [![Latest Version]][crates.io] [![Api Rustdoc]][rustdoc] [](https://github.com/mozilla/cbindgen)
|
2017-04-27 17:41:01 -04:00
|
|
|
|
2023-09-07 14:50:34 -07:00
|
|
|
[Build Status]: https://github.com/mozilla/cbindgen/workflows/cbindgen/badge.svg
|
|
|
|
[actions]: https://github.com/mozilla/cbindgen/actions
|
2017-04-27 17:41:01 -04:00
|
|
|
[Latest Version]: https://img.shields.io/crates/v/cbindgen.svg
|
|
|
|
[crates.io]: https://crates.io/crates/cbindgen
|
2017-09-29 15:49:56 -04:00
|
|
|
[Api Rustdoc]: https://img.shields.io/badge/api-rustdoc-blue.svg
|
2018-05-15 14:17:45 -07:00
|
|
|
[rustdoc]: https://docs.rs/cbindgen
|
2017-04-05 18:14:21 -04:00
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
[Read the full user docs here!](docs.md)
|
2018-09-12 18:03:41 +05:30
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
cbindgen creates C/C++11 headers for Rust libraries which expose a public C API.
|
2017-04-12 11:42:43 -04:00
|
|
|
|
2020-12-21 00:24:24 +01:00
|
|
|
While you could do this by hand, it's not a particularly good use of your time.
|
|
|
|
It's also much more likely to be error-prone than machine-generated headers that
|
|
|
|
are based on your actual Rust code. The cbindgen developers have also worked
|
|
|
|
closely with the developers of Rust to ensure that the headers we generate
|
|
|
|
reflect actual guarantees about Rust's type layout and ABI.
|
|
|
|
|
|
|
|
C++ headers are nice because we can use operator overloads, constructors, enum
|
|
|
|
classes, and templates to make the API more ergonomic and Rust-like. C headers
|
|
|
|
are nice because you can be more confident that whoever you're interoperating
|
|
|
|
with can handle them. With cbindgen *you don't need to choose*! You can just
|
|
|
|
tell it to emit both from the same Rust library.
|
|
|
|
|
|
|
|
There are two ways to use cbindgen: as a standalone program, or as a library
|
|
|
|
(presumably in your build.rs). There isn't really much practical difference,
|
|
|
|
because cbindgen is a simple rust library with no interesting dependencies.
|
|
|
|
|
|
|
|
Using it as a program means people building your software will need it
|
|
|
|
installed. Using it in your library means people may have to build cbindgen more
|
|
|
|
frequently (e.g. every time they update their rust compiler).
|
|
|
|
|
|
|
|
It's worth noting that the development of cbindgen has been largely adhoc, as
|
|
|
|
features have been added to support the usecases of the maintainers. This means
|
|
|
|
cbindgen may randomly fail to support some particular situation simply because
|
|
|
|
no one has put in the effort to handle it yet. [Please file an issue if you run
|
2023-09-07 14:50:34 -07:00
|
|
|
into such a situation](https://github.com/mozilla/cbindgen/issues/new). Although
|
2020-12-21 00:24:24 +01:00
|
|
|
since we all have other jobs, you might need to do the implementation work too
|
|
|
|
:)
|
2017-06-21 03:33:45 -04:00
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
# Quick Start
|
2017-04-18 03:14:55 -04:00
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
To install cbindgen, you just need to run
|
|
|
|
|
|
|
|
```text
|
|
|
|
cargo install --force cbindgen
|
2017-04-17 22:26:18 -04:00
|
|
|
```
|
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
(--force just makes it update to the latest cbindgen if it's already installed)
|
|
|
|
|
2022-10-10 14:43:01 -07:00
|
|
|
Or with Homebrew, run
|
|
|
|
|
|
|
|
```text
|
|
|
|
brew install cbindgen
|
|
|
|
```
|
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
To use cbindgen you need two things:
|
|
|
|
|
|
|
|
* A configuration (cbindgen.toml, which can be empty to start)
|
|
|
|
* A Rust crate with a public C API
|
2017-06-29 00:25:56 -04:00
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
Then all you need to do is run it:
|
|
|
|
|
|
|
|
```text
|
2019-07-02 17:24:48 +07:00
|
|
|
cbindgen --config cbindgen.toml --crate my_rust_library --output my_header.h
|
2017-06-29 00:12:09 -04:00
|
|
|
```
|
2017-06-21 04:39:02 -04:00
|
|
|
|
2020-05-26 16:28:25 -04:00
|
|
|
This produces a header file for C++. For C, add the `--lang c` switch.
|
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
See `cbindgen --help` for more options.
|
|
|
|
|
|
|
|
[Read the full user docs here!](docs.md)
|
|
|
|
|
2019-06-11 19:43:16 -04:00
|
|
|
[Get a template cbindgen.toml here.](template.toml)
|
2017-04-17 21:39:40 -04:00
|
|
|
|
2019-06-10 16:45:19 -04:00
|
|
|
# Examples
|
2017-09-29 16:02:55 -04:00
|
|
|
|
2020-12-21 00:24:24 +01:00
|
|
|
We don't currently have a nice tailored example application, but [the
|
|
|
|
tests](tests/rust/) contain plenty of interesting examples of our features.
|
2017-09-29 16:02:55 -04:00
|
|
|
|
2020-12-21 00:24:24 +01:00
|
|
|
You may also find it interesting to browse the projects that are using cbindgen
|
|
|
|
in production:
|
2017-11-14 21:28:30 -05:00
|
|
|
|
2017-11-14 17:43:18 -05:00
|
|
|
* [milksnake](https://github.com/getsentry/milksnake)
|
2019-06-10 16:45:19 -04:00
|
|
|
* [webrender](https://searchfox.org/mozilla-central/source/gfx/webrender_bindings) ([generated header](https://searchfox.org/mozilla-central/source/__GENERATED__/gfx/webrender_bindings/webrender_ffi_generated.h))
|
|
|
|
* [stylo](https://searchfox.org/mozilla-central/source/layout/style) ([generated header](https://searchfox.org/mozilla-central/source/__GENERATED__/layout/style/ServoStyleConsts.h))
|
2022-10-17 13:21:24 +08:00
|
|
|
* [maturin](https://github.com/PyO3/maturin)
|
2017-11-14 21:28:30 -05:00
|
|
|
|
2020-12-21 00:24:24 +01:00
|
|
|
If you're using `cbindgen` and would like to be added to this list, please open
|
|
|
|
a pull request!
|
|
|
|
|
|
|
|
# Releases
|
|
|
|
|
|
|
|
cbindgen doesn't have a fixed release calendar, please file an issue requesting
|
|
|
|
a release if there's something fixed in trunk that you need released. Ping
|
|
|
|
`@emilio` for increased effect.
|