How to use static linked C++ library via FFI #3886
-
SummaryI want to use bullet physics, a physics engine library written in c++ via FFI, in wasm rust. Additional DetailsTo do this, I found out that I need to build the c++ library to nostd with cc. So I added includes to provide std of c++ from rust to ffi // build.rs
fn main() {
cc::Build::new()
.archiver("llvm-ar")
.cpp_link_stdlib(None)
.cpp(true)
.flag("-xc++")
.includes([
"cpp_wasm_std"
])
.files([
"cpp_src/lib.h",
"cpp_src/LinearMath/btScalar.h"
])
.compile("bullet");
println!("cargo:rerun-if-changed=cpp_wasm_std");
println!("cargo:rerun-if-changed=cpp_src/lib.h");
println!("cargo:rerun-if-changed=cpp_src/LinearMath/btScalar.h");
} The build was successful but wasm-bindgen is panicking. I don't know if this is a bug: Compiling cpp-wasm-ffi v0.1.0 (D:\noname\Projects\GitHub\rust-wasm-cpp-ffi\src\wasm_src)
Finished dev [unoptimized + debuginfo] target(s) in 0.14s
[INFO]: Installing wasm-bindgen...
thread 'main' panicked at crates\wasm-interpreter\src\lib.rs:311:32:
index out of bounds: the len is 1024 but the index is 534773760
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Running the wasm-bindgen CLI
Caused by: Running the wasm-bindgen CLI
Caused by: failed to execute `wasm-bindgen`: exited with exit code: 101 Here is a repository that reproduces the issue: |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
I don't know if this is actually the bug you are hitting, but Rust See #3454. |
Beta Was this translation helpful? Give feedback.
-
It looks like LLVM's generating (func $__wbindgen_describe_init.command_export (;1496;) (type 14)
call $__wasm_call_ctors
call $__wbindgen_describe_init
) This has come up before, but only in the context of compiling to WASI (#2969). Previously I'd assumed that It seems like #2673 may actually have been the same issue since the OP there also mentioned C++, but it never went anywhere. After some quick googling for what Also note that this only happens in debug builds, it goes away in release mode. My guess as to why is that the compiler can see that the global initialisation is just setting some memory to a fixed value, and so replaces it with a data segment instead. |
Beta Was this translation helpful? Give feedback.
It looks like LLVM's generating
__wasm_call_ctors
calls again:This has come up before, but only in the context of compiling to WASI (#2969). Previously I'd assumed that
__wasm_call_ctors
was just something WASI-specific, but now I'm thinking it might just get generated automatically whenever there's code that needs global initialisation, which included all WASI code. (This is all speculation, I haven't actually double-checked this yet.)It seems like #2673 may actually have been the same issue since the OP there also mentioned C++, but it never went anywhere.
Aft…