|
Name |
|
Date |
Size |
#Lines |
LOC |
| .. | | - | - |
| .github/ | | 22-Oct-2025 | - | 756 | 548 |
| bindgen/ | | 22-Oct-2025 | - | 30,753 | 22,248 |
| bindgen-cli/ | | 22-Oct-2025 | - | 1,336 | 1,041 |
| bindgen-integration/ | | 22-Oct-2025 | - | 1,052 | 840 |
| bindgen-tests/ | | 22-Oct-2025 | - | 236,865 | 178,752 |
| book/ | | 22-Oct-2025 | - | 1,880 | 1,451 |
| ci/ | | 22-Oct-2025 | - | 207 | 146 |
| csmith-fuzzing/ | | 22-Oct-2025 | - | 641 | 502 |
| releases/ | | 22-Oct-2025 | - | 70 | 48 |
| .gitattributes | D | 22-Oct-2025 | 78 | 3 | 3 |
| .gitignore | D | 22-Oct-2025 | 403 | 26 | 20 |
| CHANGELOG.md | D | 22-Oct-2025 | 62.1 KiB | 1,871 | 1,371 |
| CONTRIBUTING.md | D | 22-Oct-2025 | 22.9 KiB | 624 | 459 |
| Cargo.lock | D | 22-Oct-2025 | 20.7 KiB | 814 | 719 |
| Cargo.toml | D | 22-Oct-2025 | 1.2 KiB | 45 | 40 |
| LICENSE | D | 22-Oct-2025 | 1.5 KiB | 30 | 23 |
| OAT.xml | D | 22-Oct-2025 | 4.5 KiB | 73 | 19 |
| README.OpenSource | D | 22-Oct-2025 | 1.1 KiB | 52 | 52 |
| README.md | D | 22-Oct-2025 | 3.2 KiB | 90 | 60 |
| README_zh.md | D | 22-Oct-2025 | 5.4 KiB | 221 | 176 |
| appveyor.yml | D | 22-Oct-2025 | 2 KiB | 58 | 48 |
| bindgen_test.png | D | 22-Oct-2025 | 54.7 KiB | | |
| clippy.toml | D | 22-Oct-2025 | 252 | 6 | 5 |
| example-graphviz-ir.png | D | 22-Oct-2025 | 1.3 MiB | | |
| rustfmt.toml | D | 22-Oct-2025 | 40 | 3 | 2 |
| triagebot.toml | D | 22-Oct-2025 | 417 | 28 | 27 |
README.OpenSource
1[
2 {
3 "Name": "bindgen",
4 "License": "BSD-3-Clause-LBNL",
5 "License File": "LICENSE",
6 "Version Number": "v0.70.1",
7 "Owner": "fangting12@huawei.com",
8 "Upstream URL": "https://github.com/rust-lang/rust-bindgen",
9 "Description": "A tool that automatically generates Rust bindings for C and C++ libraries.",
10 "Dependencies": [
11 "rust-shlex",
12 "rust-lang/glob",
13 "rust-cexpr",
14 "Human Time",
15 "BurntSushi/aho-corasick",
16 "strsim-rs",
17 "dtolnay/quote",
18 "BurntSushi/termcolor",
19 "libc",
20 "once_cell",
21 "log",
22 "regex",
23 "libloading",
24 "bitflags",
25 "nom",
26 "is-terminal",
27 "atty",
28 "lazycell",
29 "peeking_take_while",
30 "proc-macro-error",
31 "syn",
32 "minimal-lexical",
33 "env_logger",
34 "cfg-if",
35 "clang-sys",
36 "unicode-ident",
37 "os_str_bytes",
38 "which",
39 "io-lifetimes",
40 "either",
41 "memchr",
42 "clap",
43 "heck",
44 "lazy_static",
45 "proc-macro2",
46 "rustix",
47 "linux-raw-sys",
48 "rustc-hash",
49 "version_check"
50 ]
51 }
52]
README.md
1[](https://crates.io/crates/bindgen)
2[](https://docs.rs/bindgen/)
3
4# `bindgen`
5
6**`bindgen` automatically generates Rust FFI bindings to C (and some C++) libraries.**
7
8For example, given the C header `doggo.h`:
9
10```c
11typedef struct Doggo {
12 int many;
13 char wow;
14} Doggo;
15
16void eleven_out_of_ten_majestic_af(Doggo* pupper);
17```
18
19`bindgen` produces Rust FFI code allowing you to call into the `doggo` library's
20functions and use its types:
21
22```rust
23/* automatically generated by rust-bindgen 0.99.9 */
24
25#[repr(C)]
26pub struct Doggo {
27 pub many: ::std::os::raw::c_int,
28 pub wow: ::std::os::raw::c_char,
29}
30
31extern "C" {
32 pub fn eleven_out_of_ten_majestic_af(pupper: *mut Doggo);
33}
34```
35
36## Users Guide
37
38[ Read the `bindgen` users guide here! ](https://rust-lang.github.io/rust-bindgen)
39
40## MSRV
41
42The `bindgen` minimum supported Rust version is **1.70.0**.
43
44The `bindgen-cli` minimum supported Rust version is **1.70.0**.
45
46No MSRV bump policy has been established yet, so MSRV may increase in any release.
47
48The MSRV is the minimum Rust version that can be used to *compile* each crate. However, `bindgen` and `bindgen-cli` can generate bindings that are compatible with Rust versions below the current MSRV.
49
50Most of the time, the `bindgen-cli` crate will have a more recent MSRV than `bindgen` as crates such as `clap` require it.
51
52## API Reference
53
54[API reference documentation is on docs.rs](https://docs.rs/bindgen)
55
56## Environment Variables
57
58In addition to the [library API](https://docs.rs/bindgen) and [executable command-line API][bindgen-cmdline],
59`bindgen` can be controlled through environment variables.
60
61End-users should set these environment variables to modify `bindgen`'s behavior without modifying the source code of direct consumers of `bindgen`.
62
63- `BINDGEN_EXTRA_CLANG_ARGS`: extra arguments to pass to `clang`
64 - Arguments are whitespace-separated
65 - Use shell-style quoting to pass through whitespace
66 - Examples:
67 - Specify alternate sysroot: `--sysroot=/path/to/sysroot`
68 - Add include search path with spaces: `-I"/path/with spaces"`
69- `BINDGEN_EXTRA_CLANG_ARGS_<TARGET>`: similar to `BINDGEN_EXTRA_CLANG_ARGS`,
70 but used to set per-target arguments to pass to clang. Useful to set system include
71 directories in a target-specific way in cross-compilation environments with multiple targets.
72 Has precedence over `BINDGEN_EXTRA_CLANG_ARGS`.
73
74Additionally, `bindgen` uses `libclang` to parse C and C++ header files.
75To modify how `bindgen` searches for `libclang`, see the [`clang-sys` documentation][clang-sys-env].
76For more details on how `bindgen` uses `libclang`, see the [`bindgen` users guide][bindgen-book-clang].
77
78## Releases
79
80We don't follow a specific release calendar, but if you need a release please
81file an issue requesting that (ping `@emilio` for increased effectiveness).
82
83## Contributing
84
85[See `CONTRIBUTING.md` for hacking on `bindgen`!](./CONTRIBUTING.md)
86
87[bindgen-cmdline]: https://rust-lang.github.io/rust-bindgen/command-line-usage.html
88[clang-sys-env]: https://github.com/KyleMayes/clang-sys#environment-variables
89[bindgen-book-clang]: https://rust-lang.github.io/rust-bindgen/requirements.html#clang
90
README_zh.md
1[](https://crates.io/crates/bindgen)
2[](https://docs.rs/bindgen/)
3
4## 引入背景
5
6`bindgen` 自动生成Rust与C(和一些C++)库的FFI绑定。
7
8例如,给定C头文件`doggo.h`:
9
10```c
11typedef struct Doggo {
12 int many;
13 char wow;
14} Doggo;
15
16void eleven_out_of_ten_majestic_af(Doggo* pupper);
17```
18
19`bindgen`产生Rust FFI代码,允许调用`doggo`库的函数并使用其类型:
20
21```rust
22/* automatically generated by rust-bindgen 0.99.9 */
23
24#[repr(C)]
25pub struct Doggo {
26 pub many: ::std::os::raw::c_int,
27 pub wow: ::std::os::raw::c_char,
28}
29
30extern "C" {
31 pub fn eleven_out_of_ten_majestic_af(pupper: *mut Doggo);
32}
33```
34
35## 目录结构
36bindgen的目录树结构如下:
37```
38.
39├── appveyor.yml
40├── bindgen
41│ ├── BUILD.gn
42│ ├── build.rs
43│ ├── callbacks.rs
44│ ├── Cargo.toml
45│ ├── codegen
46│ ├── deps.rs
47│ ├── extra_assertions.rs
48│ ├── features.rs
49│ ├── ir
50│ ├── lib.rs
51│ ├── LICENSE -> ../LICENSE
52│ ├── log_stubs.rs
53│ ├── parse.rs
54│ ├── regex_set.rs
55│ └── time.rs
56├── bindgen-cli
57│ ├── BUILD.gn
58│ ├── Cargo.toml
59│ ├── LICENSE -> ../LICENSE
60│ ├── main.rs
61│ └── options.rs
62├── bindgen-integration
63│ ├── build.rs
64│ ├── Cargo.toml
65│ ├── cpp
66│ ├── include
67│ └── src
68├── bindgen-tests
69│ ├── build.rs
70│ ├── Cargo.toml
71│ ├── src
72│ └── tests
73├── book
74│ ├── book.toml
75│ └── src
76├── Cargo.lock
77├── Cargo.toml
78├── CHANGELOG.md
79├── ci
80│ ├── assert-no-diff.bat
81│ ├── no-includes.sh
82│ ├── test.bat
83│ └── test.sh
84├── CONTRIBUTING.md
85├── csmith-fuzzing
86│ ├── csmith.h
87│ ├── driver.py
88│ ├── predicate.py
89│ └── README.md
90├── example-graphviz-ir.png
91├── LICENSE
92├── OAT.xml
93├── README.md
94├── README.OpenSource
95├── README_zh.md
96├── releases
97│ ├── friends.sh
98│ └── release-announcement-template.md
99└── rustfmt.toml
100```
101
102## Bindgen工具在OH上的使用指导
103
104### 操作步骤
105下面是一个使用bindgen实现Rust调用C的示例。
106
1071. 在C代码侧,使用头文件lib.h定义两个接口,接口FuncAAddB用来实现两数求和,接口SayHello用来打印字符串。
108
109 ```c
110 #ifndef BUILD_RUST_TESTS_BINDGEN_TEST_LIB_H_
111 #define BUILD_RUST_TESTS_BINDGEN_TEST_LIB_H_
112 #include <stdint.h>
113 #include "build/rust/tests/test_bindgen_test/test_for_hello_world/lib2.h"
114
115 uint32_t FuncAAddB(uint32_t a, uint32_t b);
116 void SayHello(const char *message);
117
118 #endif // BUILD_RUST_TESTS_BINDGEN_TEST_LIB_H_
119 ```
120
121
1222. 在lib.c中添加对两个接口的对应实现。
123
124 ```c
125 #include "build/rust/tests/test_bindgen_test/test_for_hello_world/lib.h"
126 #include <stdint.h>
127 #include <stdio.h>
128
129 void SayHello(const char *message)
130 {
131 printf("This is a test for bindgen hello world:\n");
132 printf("%s\n", message);
133 }
134
135 uint32_t FuncAAddB(uint32_t a, uint32_t b)
136 {
137 printf("This is a test for bindgen of a + b:\n");
138 return a + b;
139 }
140 ```
141
1423. 添加文件main.rs,就可以在Rust侧通过c_ffi实现对C侧的接口调用。注意Rust侧调用的不安全接口需要使用unsafe封装。
143
144 ```rust
145 //! bindgen test for hello world
146 #![allow(clippy::approx_constant)]
147 mod c_ffi {
148 #![allow(dead_code)]
149 #![allow(non_upper_case_globals)]
150 #![allow(non_camel_case_types)]
151 include!(env!("BINDGEN_RS_FILE"));
152 }
153 /// pub fn add_two_numbers_in_c
154 pub fn add_two_numbers_in_c(a: u32, b: u32) -> u32 {
155 unsafe { c_ffi::FuncAAddB(a, b) }
156 }
157
158 use std::ffi::c_char;
159 use std::ffi::CString;
160
161 /// fn main()
162 fn main() {
163 println!("{} + {} = {}", 3, 7, add_two_numbers_in_c(3, 7));
164 let c_str = CString::new("This is a message from C").unwrap();
165 let c_world: *const c_char = c_str.as_ptr() as *const c_char;
166 unsafe {
167 c_ffi::SayHello(c_world);
168 }
169 }
170
171 ```
172
1734. 添加构建文件BUILD.gn,建立Rust模块对C模块的依赖。
174
175 ```GN
176 import("//build/ohos.gni")
177
178 ohos_shared_library("c_lib") {
179 sources = [ "lib.c" ]
180 defines = [ "COMPONENT_IMPLEMENTATION" ]
181 }
182
183 rust_bindgen("c_lib_bindgen") {
184 header = "lib.h"
185 }
186
187 ohos_rust_executable("bindgen_test") {
188 deps = [ ":c_lib" ]
189 deps += [ ":c_lib_bindgen" ]
190 sources = [ "main.rs" ]
191 bindgen_output = get_target_outputs(":c_lib_bindgen")
192 inputs = bindgen_output
193 rustenv = [ "BINDGEN_RS_FILE=" + rebase_path(bindgen_output[0]) ]
194 crate_root = "main.rs"
195 }
196 ```
197
198**调测验证**
199
200
201
202## MSRV
203
204Minimum support Rust版本是**1.60.0**。
205
206目前还没有制定MSRV提升政策,所以MSRV可能会在任何版本中增加。
207
208MSRV是可用于编译`bindgen`的Minimum Rust版本。`bindgen`可以生成与低于当前MSRV的Rust版本兼容的绑定。
209
210## API参考
211
212[API参考文档在docs.rs上](https://docs.rs/bindgen)
213
214## 开发者贡献
215
216在使用该工具的过程中有任何问题欢迎开发者在社区issue中反馈。
217
218<br>
219
220
221