How to get a C header-only library to compile for wasm32 with cargo web

I am using the stb_image crate in a project of mine and I’m trying to get it working as wasm32 with cargo web.

stb_image has some issues because it still uses the libc types so I forked it and switched to std::os::raw and updated it to use the latest cc-rs.

But when I try to compile for wasm32 with cargo web I get the following error:

error: failed to run custom build command for `stb_image v0.2.2 (/Users/tanis/Documents/tanis2000-rust-stb-image)`
process didn't exit successfully: `/Users/tanis/Documents/tanis2000-rust-stb-image/target/debug/build/stb_image-a85c27b930fed555/build-script-build` (exit code: 101)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-apple-darwin")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
DEBUG = Some("true")
running: "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-o" "/Users/tanis/Documents/tanis2000-rust-stb-image/target/wasm32-unknown-unknown/debug/build/stb_image-f82d8dbbea50779c/out/src/stb_image.o" "-c" "src/stb_image.c"
cargo:warning=src/stb_image.c:390:10: fatal error: 'stdio.h' file not found
cargo:warning=#include <stdio.h>
cargo:warning=         ^~~~~~~~~
cargo:warning=1 error generated.
exit code: 1

--- stderr
thread 'main' panicked at '

Internal error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-o" "/Users/tanis/Documents/tanis2000-rust-stb-image/target/wasm32-unknown-unknown/debug/build/stb_image-f82d8dbbea50779c/out/src/stb_image.o" "-c" "src/stb_image.c" with args "clang" did not execute successfully (status code exit code: 1).

', /Users/tanis/.cargo/registry/src/
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

warning: build failed, waiting for other jobs to finish...
error: build failed
error: build failed

For all I can understand from the error message it looks like either cc-rs doesn’t know where the sysroot for wasm32 is or it’s passing wrong arguments to clang, but I’m in no way an expert so I really need some help.

You can check the full discussion about this issue on the stb_image repo:

Thank you all in advance.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.