7.1.1 简单的 C 库
01-简单的 C 库 — Comprehensive Rust
译文 · 基于 Comprehensive Rust
原文链接: https://google.github.io/comprehensive-rust/android/interoperability/with-c/c-library.html
7.1.1 简单的 C 库
先创建一个小的 C 库:
interoperability/bindgen/libbirthday.h:
1
2
3
4
5
6
| typedef struct card {
const char* name;
int years;
} card;
void print_card(const card* card);
|
interoperability/bindgen/libbirthday.c:
1
2
3
4
5
6
7
8
9
| #include <stdio.h>
#include "libbirthday.h"
void print_card(const card* card) {
printf("+--------------\n");
printf("| Happy Birthday %s!\n", card->name);
printf("| Congratulations with the %i years!\n", card->years);
printf("+--------------\n");
}
|
把它加到你的 Android.bp 文件中:
interoperability/bindgen/Android.bp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| cc_library {
name: "libbirthday",
srcs: ["libbirthday.c"],
}
rust_bindgen {
name: "libbirthday_bindgen",
crate_name: "birthday_bindgen",
wrapper_src: "libbirthday_wrapper.h",
source_stem: "bindings",
static_libs: ["libbirthday"],
}
rust_test {
name: "libbirthday_bindgen_test",
srcs: [":libbirthday_bindgen"],
crate_name: "libbirthday_bindgen_test",
test_suites: ["general-tests"],
auto_gen_config: true,
clippy_lints: "none", // 生成的文件,跳过 lint
lints: "none",
}
|