You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: component-model/src/language-support/c.md
+33-18
Original file line number
Diff line number
Diff line change
@@ -10,21 +10,21 @@ First, install the CLI for [`wit-bindgen`](https://github.com/bytecodealliance/w
10
10
11
11
The WASI SDK will install a local version of `clang` configured with a wasi-sysroot. Follow [these instructions](https://github.com/WebAssembly/wasi-sdk#use) to configure it for use. Note that you can also use your installed system or emscripten `clang` by building with `--target=wasm32-wasi` but you will need some artifacts from WASI SDK to enable and link that build target (more information is available in WASI SDK's docs).
12
12
13
-
Start by generating a C skeleton from `wit-bindgen` using the [sample `add.wit` file](../../examples/example-host/add.wit):
13
+
Start by generating a C skeleton from `wit-bindgen` using the [sample `add.wit` file](https://github.com/bytecodealliance/component-docs/tree/main/component-model/examples/example-host/add.wit):
14
14
```sh
15
15
>wit-bindgen c add.wit
16
16
Generating "example.c"
17
17
Generating "example.h"
18
18
Generating "example_component_type.o"
19
19
```
20
20
21
-
This has generated several files - an `example.h` (based on the name of your `world`) with the prototype of the `add` function - `int32_t example_add(int32_t x, int32_t y);`, as well as some generated code in `example.c` that interfaces with the component model ABI to call your function. Additionally, `example_component_type.o` contains object code referenced in `example.c` from an `extern` that must be linked via clang.
21
+
This has generated several files - an `example.h` (based on the name of your `world`) with the prototype of the `add` function (prefixed by `exports_`) - `int32_t exports_example_add(int32_t x, int32_t y);`, as well as some generated code in `example.c` that interfaces with the component model ABI to call your function. Additionally, `example_component_type.o` contains object code referenced in `example.c` from an `extern` that must be linked via clang.
22
22
23
23
Next, create an `add.c` that implements your function defined in `example.h`:
24
24
```c
25
25
#include"example.h"
26
26
27
-
int32_texample_add(int32_t x, int32_t y)
27
+
int32_texports_example_add(int32_t x, int32_t y)
28
28
{
29
29
return x + y;
30
30
}
@@ -49,7 +49,7 @@ For example, modifying the above to reference `printf()` would compile:
49
49
#include"example.h"
50
50
#include<stdio.h>
51
51
52
-
int32_texample_add(int32_t x, int32_t y)
52
+
int32_texports_example_add(int32_t x, int32_t y)
53
53
{
54
54
int32_t result = x + y;
55
55
printf("%d", result);
@@ -81,22 +81,37 @@ Finally, you can inspect the embedded wit to see your component (including any W
You must use the `wasi_snapshot_preview1.wasm` from the same version of wasmtime that the host is using to ensure the WASI interface versions match. Additionally, the host must explicitly [add WASI to the linker](https://docs.wasmtime.dev/api/wasmtime_wasi/fn.add_to_linker_sync.html) to run the app. If these are not configured correctly, you may see errors like the following:
0: component imports instance `wasi:io/[email protected]`, but a matching implementation was not found in the linker
113
+
1: instance export`error` has the wrong type
114
+
2: resource implementation is missing
100
115
```
101
116
102
117
### Running a Component from C/C++ Applications
@@ -105,4 +120,4 @@ It is not yet possible to run a Component using the `wasmtime` `c-api` - [see th
105
120
106
121
However, C/C++ language guest components can be composed with components written in any other language and run by their toolchains, or even composed with a C language command component and run via the `wasmtime` CLI or any other host.
107
122
108
-
See the [Rust Tooling guide](../language-support/rust.md#running-a-component-from-rust-applications) for instructions on how to run this component from the Rust `example-host`.
123
+
See the [Rust Tooling guide](../language-support/rust.md#running-a-component-from-rust-applications) for instructions on how to run this component from the Rust `example-host` (replacing the path to `add.wasm` with your `add-component` above).
0 commit comments