Skip to content

Proof of concept C host#334

Open
ohAitch wants to merge 7 commits intobytecodealliance:mainfrom
ohAitch:example-c-host
Open

Proof of concept C host#334
ohAitch wants to merge 7 commits intobytecodealliance:mainfrom
ohAitch:example-c-host

Conversation

@ohAitch
Copy link
Copy Markdown
Contributor

@ohAitch ohAitch commented Mar 5, 2026

cd component-model/examples/example-c-host
gcc -o host host.c -lwasmtime && ./host 6 7 adder.wasm

Previous discussion in bytecodealliance/wasmtime#6987 (comment)

Documentation in particular needs work.

@vados-cosmonic
Copy link
Copy Markdown
Collaborator

I'm not a C expert so it's probably going to take a while (and we probably need a set of different eyes on this) but thanks for this1

@vados-cosmonic
Copy link
Copy Markdown
Collaborator

A bit late but looking some more at this, to get this across the line we would need to:

  • Update the C "run the component from ..." section (here we can likely have a C section and a C++ section)
  • Replace the TODOs in the PR
  • Add the C host to CI to catch if it breaks
  • Fix the wording around embeddings, hosts and WASI runtimes (C/C++ & JS can have embeddings of hosts, and other langauges just run wasmtime from CLI which is NOT an embedding... wasmtime is a host and has a WASI runtime)

We will definitely step in to help finish this up @ohAitch so no need to worry about it -- after chatting it looks like there are a few more changes that need to be made documentation wise to make everything coherent.

Copy link
Copy Markdown
Collaborator

@kate-goldenring kate-goldenring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for putting this together! I put copilot to work to help make this pretty close to ready, filling in the docs and making the Dockerfile cross arch. I PR'd it into your branch here ohAitch#1 for you to review. Feel free to pull that in instead of directly resolving my suggestions.

Comment thread component-model/examples/example-c-host/Dockerfile.guest_and_host Outdated
@@ -0,0 +1,2 @@
@_default:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change this to a Makefile since those are used more broadly? Maybe add build and run targets. Or we remove it for now

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file is more than the example host -- it is all in one, building the guest and the host, which i think is great, but a host-only one where you could pass in your adder.wasm may also be useful. I'll PR in a suggestion

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep with #333 in it should totally just use the checked in one.

@ohAitch
Copy link
Copy Markdown
Contributor Author

ohAitch commented Apr 15, 2026

Oh thanks for looking into this, I totally missed the first update comment.

ohAitch and others added 5 commits April 15, 2026 23:06
TODO deduplicate with add.wasm see bytecodealliance#333

```sh
cd component-model/examples/example-c-host
gcc -o host host.c -lwasmtime && ./host 6 7 adder.wasm
```

Co-authored-by: Shelley <shelley@exe.dev>
I had some difficulty setting up the C toolchains, here's everything pinned down as a sanity check

```sh
docker build -t example-c-host .
docker run example-c-host
```

Co-authored-by: Shelley <shelley@exe.dev>
Documentation is not my forté but the current "running things from C is impossible due to #issue (closed)" is _definitely_ out of date.

Needs revision into a more helpful pointer to the code introduced in this PR, whatever its project name and contents shall end up being. Possibly broken out into a separate .md fragments like the rust one, possibly that was only necessary to share instructions with rust.md and the existence of the c/c++ host can stay local to this file.
- Fills in documentation on how to use the C host
- Updates Dockerfile to be cross architecture
- Adds a new Dockerfile that is just the host

Signed-off-by: Kate Goldenring <kgoldenr@akamai.com>
Signed-off-by: Kate Goldenring <kgoldenr@akamai.com>
@kate-goldenring kate-goldenring marked this pull request as ready for review April 15, 2026 23:10
exe.dev user and others added 2 commits April 15, 2026 23:11
…volume mount

- Delete Dockerfile.guest_and_host (broken wasi-sdk arch mapping; unnecessary
  now that upstream add.wasm is available via rebase)
- Rename Dockerfile.host → Dockerfile
- Fix missing trailing newline
- Update c.md: remove guest_and_host references, fix Docker instructions
  to use volume mount with $(pwd)-relative path

Co-authored-by: Shelley <shelley@exe.dev>
Tests both the native build (gcc + wasmtime C API) and the Docker
workflow against the checked-in add.wasm.

Co-authored-by: Shelley <shelley@exe.dev>
Copy link
Copy Markdown
Collaborator

@kate-goldenring kate-goldenring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ohAitch thank you for getting these docs updated. This LGTM but I'll wait for your final thumbs up before merging

@ohAitch
Copy link
Copy Markdown
Contributor Author

ohAitch commented Apr 15, 2026

Changes incorporated and rebased, no more TODOs in files I've touched, host-only cross-platform dockerfile, tyvm kate. CI green though you may wish to drop one of the two. Seems like a good time to unDraft this.

How are we doing on

wording around embeddings, hosts and WASI runtimes

@kate-goldenring
Copy link
Copy Markdown
Collaborator

@ohAitch switching to an "embedding" wording would be a nice add -- we've been misusing the word "host" throughout the docs. See an example in this pr which will likely be closed thanks to this one #337

Copy link
Copy Markdown
Collaborator

@kate-goldenring kate-goldenring left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some suggestions of how to avoid "host" wording

wasmtime --invoke 'add(2,2)' adder.wasm
```

## 7. Run the component from the example C host
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## 7. Run the component from the example C host
## 7. Run the component from C/C++ Applications


You can either use a Dockerfile to execute your add component with the C application or directly run the application.

### Option A: Compile and run the host directly
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### Option A: Compile and run the host directly
### Option A: Run the C application directly

### Option A: Compile and run the host directly

If the Wasmtime C API headers and library are installed on your system,
you can compile and run the host directly:
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
you can compile and run the host directly:
you can compile and run the application directly:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the "host" wording here be renamed "application" both in file naming and in content

@@ -0,0 +1,127 @@
/**
* C host for the adder WebAssembly component.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* C host for the adder WebAssembly component.
* C application that embeds Wasmtime and executes a component that does addition

## 6. Run the component with `wasmtime --invoke`

The following section requires you to have [a Rust toolchain][rust] installed.
If you want to quickly run the `add` export without writing a host application that embeds Wasmtime,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If you want to quickly run the `add` export without writing a host application that embeds Wasmtime,
If you want to quickly run the `add` export without writing an application that embeds Wasmtime,

## 7. Run the component from the example C host

This repository includes a C application that can execute components that implement the add interface. This application embeds Wasmtime using the Wasmtime C API:
`component-model/examples/example-c-host/host.c`.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
`component-model/examples/example-c-host/host.c`.
`component-model/examples/example-c-application/app.c`.

The application expects three arguments: the two numbers to add and the Wasm component that executed the addition. For example:

```sh
./adder-host <x> <y> <path-to-component.wasm>
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
./adder-host <x> <y> <path-to-component.wasm>
./app <x> <y> <path-to-component.wasm>

Comment on lines +360 to +362
cd component-model/examples/example-c-host
gcc -o adder-host host.c -lwasmtime
./adder-host 1 2 /absolute/path/to/adder.wasm
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
cd component-model/examples/example-c-host
gcc -o adder-host host.c -lwasmtime
./adder-host 1 2 /absolute/path/to/adder.wasm
cd component-model/examples/example-c-application
gcc -o app app.c -lwasmtime
./app 1 2 /absolute/path/to/adder.wasm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants