New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to run wasm module built from Rust with C++ dependency. Error: Can't resolve 'env' #741
Comments
You will have to have a C interface (with extern C) so that you can call wasm functions from and you will have to get a custom malloc that works with wasm |
Thank you @GreatIndieDeveloper for your reply. But as far as I understand both points should be fine. Your first point is that I need a C interface. But as far as I understood, the C interface is defined in the file And the second point is that I should provide a malloc function. Since I'm not using any includes from the c or c++ standard library, which normally implements malloc, I thought that I don't have to provide that functionality. The code is just a simple function that works on integers, which doesn't require any OS functions. Did I get your points right? Or did you mean something else? And shouldn't it have the same problems with the file having a Correct me if I'm wrong. |
Okay the problem seems to be the fact that c++ uses a different ABI and the symbol for the function has a mangled name, that the rust compiler doesn't know. One has to explicitly tell the rust compiler the mangled function name it has to link. This can be done with the attribute |
When I compile below C code with the
|
It seems like it cannot find the function malloc to link it into the .wasm file. That's why it uses the import "env". Are you compiling your file as a c++ file? If yes you I guess you should use |
I was not using c++ but want to compile sqlite. I got a |
I was trying to compile Anyways, I did that, and it comes up with this exact error. I used the default example of the Seems this must still be a problem. There's nothing wrong with the dependency at all as it is a well known library that works, and compiles fine to Rust on a non-WASM target |
Hi @TjeuKayim , did you solve the issue? |
No. If I recall correctly, I ended up not using wasm-pack |
🐛 Bug description
npm run start
fails when running a wasm module that was built from Rust with C++ dependency usingwasm-pack
. Inspecting the generated wasm module shows that it tries to import(import "env" "double_it" (func $double_it (type $t5)))
from a module "env" that it doesn't know ("double_it" is the function in the C++ dependency). The C++ dependency is actually a C file which has been renamed to have a ".cpp" ending.npm run start
works with the ".c" file. So maybe the ".cpp" ending triggers different compilation flags that yield the different result. Since both C and C++ compile to wasm I thought there should be no difference in the usage.A same problem has been initially encountered also for the ".c" file in the comment: #621 (comment). The problem was potentially resolved with a newer clang or Rust update.
Project available here: https://github.com/JanKaul/simple-wasm
🤔 Expected Behavior
npm run start
should succeed.👟 Steps to reproduce
cc
crate, which determines compiler automatically (I guessclang
)wasm-pack build
succeedsnpm install
succeedsnpm run strart
fails with following error:🌍 Your environment
wasm-pack 0.8.1
rustc 1.39.0 (4560ea788 2019-11-04)
The text was updated successfully, but these errors were encountered: