2026-07-12
Stack Overflow: View Question
Tags: tooling-recommendation, c, rust, webassembly, trunk-rs
Score: 1 | Views: 6
The asker wants to consume a C library (a fork of cubiomes, a Minecraft world-generation library that depends on libc) from a Rust web app built with trunk-rs and eframe/egui, targeting wasm32-unknown-unknown. They know a Rust cubiomes crate exists, but want the newer C fork.
Why this is genuinely hard. Trunk is only orchestration — it invokes cargo build --target wasm32-unknown-unknown and bundles the result. The real problem lives one layer down: you need a C toolchain that emits WebAssembly, and wasm32-unknown-unknown deliberately has no libc. Any C code that includes <stdio.h>, <stdlib.h>, malloc, or file I/O will fail to link because the symbols don't exist in that target.
The direction I'd take.
build.rs using the cc crate to compile the C sources. On the wasm target, cc will look for clang (not gcc) since only clang has a mature wasm backend. Install a recent clang and expose it via CC_wasm32_unknown_unknown=clang and AR_wasm32_unknown_unknown=llvm-ar.wasm32-unknown-unknown to wasm32-wasi if possible — this gives you a real libc (wasi-libc) and most portable C compiles cleanly. Trunk has some support here, but eframe/egui's web integration is built around wasm32-unknown-unknown, so this may not be a drop-in swap.wasm32-unknown-unknown, use the wasi-sysroot from the wasi-sdk and pass --sysroot=/path/to/wasi-sysroot plus -D_WASI_EMULATED_SIGNAL-style shims via cc::Build::flag(...). You'll still have to stub out anything that touches syscalls (file open, threads, time) that cubiomes doesn't actually use at runtime.bindgen, again from build.rs, pointing at the C headers.Gotchas:
fopen/fprintf/malloc paths; you may need to #define those out or provide weak stubs.build.rs, run cargo clean -p your-crate — a stale OUT_DIR object file will silently mask fixes.cubiomes crate exists precisely because someone already solved this. Skim its build.rs — even for the older upstream, its wasm handling is a working reference to fork.cc/bindgen in build.rs.
