mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-21 23:45:42 +02:00
Split C interop slides into smaller slides. (#2645)
This commit is contained in:
parent
63d716de90
commit
49e4efcd9e
@ -260,7 +260,10 @@
|
|||||||
- [Logging](android/logging.md)
|
- [Logging](android/logging.md)
|
||||||
- [Interoperability](android/interoperability.md)
|
- [Interoperability](android/interoperability.md)
|
||||||
- [With C](android/interoperability/with-c.md)
|
- [With C](android/interoperability/with-c.md)
|
||||||
- [Calling C with Bindgen](android/interoperability/with-c/bindgen.md)
|
- [A Simple C Library](android/interoperability/with-c/c-library.md)
|
||||||
|
- [Bindgen](android/interoperability/with-c/bindgen.md)
|
||||||
|
- [Running Our Binary](android/interoperability/with-c/run-our-binary.md)
|
||||||
|
- [A Simple Rust Library](android/interoperability/with-c/rust-library.md)
|
||||||
- [Calling Rust from C](android/interoperability/with-c/rust.md)
|
- [Calling Rust from C](android/interoperability/with-c/rust.md)
|
||||||
- [With C++](android/interoperability/cpp.md)
|
- [With C++](android/interoperability/cpp.md)
|
||||||
- [The Bridge Module](android/interoperability/cpp/bridge.md)
|
- [The Bridge Module](android/interoperability/cpp/bridge.md)
|
||||||
|
@ -3,28 +3,6 @@
|
|||||||
The [bindgen](https://rust-lang.github.io/rust-bindgen/introduction.html) tool
|
The [bindgen](https://rust-lang.github.io/rust-bindgen/introduction.html) tool
|
||||||
can auto-generate bindings from a C header file.
|
can auto-generate bindings from a C header file.
|
||||||
|
|
||||||
First create a small C library:
|
|
||||||
|
|
||||||
_interoperability/bindgen/libbirthday.h_:
|
|
||||||
|
|
||||||
```c
|
|
||||||
{{#include bindgen/libbirthday.h:card}}
|
|
||||||
```
|
|
||||||
|
|
||||||
_interoperability/bindgen/libbirthday.c_:
|
|
||||||
|
|
||||||
```c
|
|
||||||
{{#include bindgen/libbirthday.c:print_card}}
|
|
||||||
```
|
|
||||||
|
|
||||||
Add this to your `Android.bp` file:
|
|
||||||
|
|
||||||
_interoperability/bindgen/Android.bp_:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
{{#include bindgen/Android.bp:libbirthday}}
|
|
||||||
```
|
|
||||||
|
|
||||||
Create a wrapper header file for the library (not strictly needed in this
|
Create a wrapper header file for the library (not strictly needed in this
|
||||||
example):
|
example):
|
||||||
|
|
||||||
@ -34,8 +12,6 @@ _interoperability/bindgen/libbirthday_wrapper.h_:
|
|||||||
{{#include bindgen/libbirthday_wrapper.h:include}}
|
{{#include bindgen/libbirthday_wrapper.h:include}}
|
||||||
```
|
```
|
||||||
|
|
||||||
You can now auto-generate the bindings:
|
|
||||||
|
|
||||||
_interoperability/bindgen/Android.bp_:
|
_interoperability/bindgen/Android.bp_:
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
@ -56,24 +32,6 @@ _interoperability/bindgen/main.rs_:
|
|||||||
{{#include bindgen/main.rs:main}}
|
{{#include bindgen/main.rs:main}}
|
||||||
```
|
```
|
||||||
|
|
||||||
Build, push, and run the binary on your device:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
{{#include ../../build_all.sh:print_birthday_card}}
|
|
||||||
```
|
|
||||||
|
|
||||||
Finally, we can run auto-generated tests to ensure the bindings work:
|
|
||||||
|
|
||||||
_interoperability/bindgen/Android.bp_:
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
{{#include bindgen/Android.bp:libbirthday_bindgen_test}}
|
|
||||||
```
|
|
||||||
|
|
||||||
```shell
|
|
||||||
{{#include ../../build_all.sh:libbirthday_bindgen_test}}
|
|
||||||
```
|
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
|
|
||||||
- The Android build rules will automatically call `bindgen` for you behind the
|
- The Android build rules will automatically call `bindgen` for you behind the
|
||||||
|
23
src/android/interoperability/with-c/c-library.md
Normal file
23
src/android/interoperability/with-c/c-library.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# A Simple C Library
|
||||||
|
|
||||||
|
Let's first create a small C library:
|
||||||
|
|
||||||
|
_interoperability/bindgen/libbirthday.h_:
|
||||||
|
|
||||||
|
```c
|
||||||
|
{{#include bindgen/libbirthday.h:card}}
|
||||||
|
```
|
||||||
|
|
||||||
|
_interoperability/bindgen/libbirthday.c_:
|
||||||
|
|
||||||
|
```c
|
||||||
|
{{#include bindgen/libbirthday.c:print_card}}
|
||||||
|
```
|
||||||
|
|
||||||
|
Add this to your `Android.bp` file:
|
||||||
|
|
||||||
|
_interoperability/bindgen/Android.bp_:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{{#include bindgen/Android.bp:libbirthday}}
|
||||||
|
```
|
19
src/android/interoperability/with-c/run-our-binary.md
Normal file
19
src/android/interoperability/with-c/run-our-binary.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Running Our Binary
|
||||||
|
|
||||||
|
Build, push, and run the binary on your device:
|
||||||
|
|
||||||
|
```shell
|
||||||
|
{{#include ../../build_all.sh:print_birthday_card}}
|
||||||
|
```
|
||||||
|
|
||||||
|
Finally, we can run auto-generated tests to ensure the bindings work:
|
||||||
|
|
||||||
|
_interoperability/bindgen/Android.bp_:
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{{#include bindgen/Android.bp:libbirthday_bindgen_test}}
|
||||||
|
```
|
||||||
|
|
||||||
|
```shell
|
||||||
|
{{#include ../../build_all.sh:libbirthday_bindgen_test}}
|
||||||
|
```
|
23
src/android/interoperability/with-c/rust-library.md
Normal file
23
src/android/interoperability/with-c/rust-library.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
# A Simple Rust Library
|
||||||
|
|
||||||
|
Exporting Rust functions and types to C is easy. Here's a simple Rust library:
|
||||||
|
|
||||||
|
_interoperability/rust/libanalyze/analyze.rs_
|
||||||
|
|
||||||
|
```rust,editable
|
||||||
|
{{#include rust/libanalyze/analyze.rs:analyze_numbers}}
|
||||||
|
```
|
||||||
|
|
||||||
|
_interoperability/rust/libanalyze/Android.bp_
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
{{#include rust/libanalyze/Android.bp}}
|
||||||
|
```
|
||||||
|
|
||||||
|
<details>
|
||||||
|
|
||||||
|
`#[unsafe(no_mangle)]` disables Rust's usual name mangling, so the exported
|
||||||
|
symbol will just be the name of the function. You can also use
|
||||||
|
`#[unsafe(export_name = "some_name")]` to specify whatever name you want.
|
||||||
|
|
||||||
|
</details>
|
@ -1,12 +1,6 @@
|
|||||||
# Calling Rust
|
# Calling Rust
|
||||||
|
|
||||||
Exporting Rust functions and types to C is easy:
|
We can now call this from a C binary:
|
||||||
|
|
||||||
_interoperability/rust/libanalyze/analyze.rs_
|
|
||||||
|
|
||||||
```rust,editable
|
|
||||||
{{#include rust/libanalyze/analyze.rs:analyze_numbers}}
|
|
||||||
```
|
|
||||||
|
|
||||||
_interoperability/rust/libanalyze/analyze.h_
|
_interoperability/rust/libanalyze/analyze.h_
|
||||||
|
|
||||||
@ -14,14 +8,6 @@ _interoperability/rust/libanalyze/analyze.h_
|
|||||||
{{#include rust/libanalyze/analyze.h:analyze_numbers}}
|
{{#include rust/libanalyze/analyze.h:analyze_numbers}}
|
||||||
```
|
```
|
||||||
|
|
||||||
_interoperability/rust/libanalyze/Android.bp_
|
|
||||||
|
|
||||||
```javascript
|
|
||||||
{{#include rust/libanalyze/Android.bp}}
|
|
||||||
```
|
|
||||||
|
|
||||||
We can now call this from a C binary:
|
|
||||||
|
|
||||||
_interoperability/rust/analyze/main.c_
|
_interoperability/rust/analyze/main.c_
|
||||||
|
|
||||||
```c
|
```c
|
||||||
@ -39,11 +25,3 @@ Build, push, and run the binary on your device:
|
|||||||
```shell
|
```shell
|
||||||
{{#include ../../build_all.sh:analyze_numbers}}
|
{{#include ../../build_all.sh:analyze_numbers}}
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
|
||||||
|
|
||||||
`#[unsafe(no_mangle)]` disables Rust's usual name mangling, so the exported
|
|
||||||
symbol will just be the name of the function. You can also use
|
|
||||||
`#[unsafe(export_name = "some_name")]` to specify whatever name you want.
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
@ -15,8 +15,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
// ANCHOR: analyze_numbers
|
// ANCHOR: analyze_numbers
|
||||||
#ifndef ANALYSE_H
|
#ifndef ANALYZE_H
|
||||||
#define ANALYSE_H
|
#define ANALYZE_H
|
||||||
|
|
||||||
void analyze_numbers(int x, int y);
|
void analyze_numbers(int x, int y);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user