I've tried using both cargo component and manual wit-bindgen, but both give me an error error: module requires an import interface named
env`` converting a preview1 wasm module to a preview2 wasm component , and I can't find a reference to it in the example
You can use e.g. wasm-tools print
to see what functions the module is importing from env
, which might point to what's wrong.
Interesting, I guess it's these:
(import "env" "_Znwm" (func $_Znwm (;0;) (type 4)))
(import "env" "_ZdlPv" (func $_ZdlPv (;1;) (type 0)))
(import "env" "__cxa_allocate_exception" (func $__cxa_allocate_exception (;2;) (type 4)))
(import "env" "_ZNSt12length_errorD1Ev" (func $_ZNSt12length_errorD1Ev (;3;) (type 4)))
(import "env" "__cxa_throw" (func $__cxa_throw (;4;) (type 6)))
(import "env" "_ZNSt20bad_array_new_lengthD1Ev" (func $_ZNSt20bad_array_new_lengthD1Ev (;5;) (type 4)))
(import "env" "_ZNSt20bad_array_new_lengthC1Ev" (func $_ZNSt20bad_array_new_lengthC1Ev (;6;) (type 4)))
(import "env" "_ZnwmSt11align_val_t" (func $_ZnwmSt11align_val_t (;20;) (type 3)))
(import "env" "_ZdlPvSt11align_val_t" (func $_ZdlPvSt11align_val_t (;21;) (type 2)))
Did you by chance link C++ code using the clang
command? If so, you may need to link with the clang++
command.
Yeah, this was the build command
RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang cargo component build
Yeah; try changing the clang
at the end of the CXX
to clang++
Context: this rust library pulls in a wrapper crate for tree-sitter, which is written (partly) in C++
This gives the same error and the same env lines
RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang++ cargo component build
IIRC, the linker was rust-lld
I did get that error at one point that mentioned rust-lld
This is the repo if anyone wants to try: https://github.com/benwis/femark/tree/wasi
Running on latest Rust stable on Arch Linux
Joel Dice said:
IIRC, the linker was rust-lld
I could tell cargo to try using an alternate linker, is the lld in the same folder the recommended one?
I'd guess rust-lld is fine, but what's needed is to link in the C++ standard library. You may need to add something like -lc++ -lc++abi
to the link line..
Dan Gohman said:
I'd guess rust-lld is fine, but what's needed is to link in the C++ standard library. You may need to add something like
-lc++ -lc++abi
to the link line..
For a C build noob, where would I add that?
Ok I tried adding it to RUSTFLAGS like so
RUSTFLAGS='-L /opt/wasi-sdk-21.0/share/wasi-sysroot/lib/wasm32-wasi -lstatic=c++ -lstatic=c++abi' CXXSTDLIB=c++ CC=/opt/wasi-sdk-21.0/bin/clang CXX=/opt/wasi-sdk-21.0/bin/clang++ cargo component build
it seems to throw the same error, I'll play with this a bit. I don't understand where the env interface comes from and what controls what goes into it
A thought occurs to me that this might not be a cargo issue, since it successfully builds
Compiling wit-parser v0.200.0
Compiling wasm-metadata v0.200.0
Compiling tree-sitter-highlight v0.20.1
Compiling wit-bindgen-core v0.19.1
Compiling wit-component v0.200.0
Compiling femark v0.1.3 (/celCluster/projects/femark)
Finished dev [unoptimized + debuginfo] target(s) in 10.67s
Creating component /celCluster/projects/femark/target/wasm32-wasi/debug/femark.wasm
error: module requires an import interface named `env`
femark on wasi [$!?] is 📦 v0.1.3 via 🦀 v1.76.0 took 10s
and it's only the conversion to preview2 that's failing
Fixed it, turns out I needed to set CXXFLAGS=-fno-exceptions
and that cleared it right up
Last updated: Dec 23 2024 at 13:07 UTC