当我使用 opencascade = "0.2.0" 依赖项时发生错误。 以下是我的 Cargo.toml
[package]
name = "wasm-demo"
version = "0.1.0"
authors = ["Ravi"]
edition = "2018"
[env]
LIBC_INCLUDE_DIR = { value = "/usr/include/c++/11", force = true, relative = false }
build = "build.rs"
[lib]
crate-type = ["cdylib"]
[dependencies]
console_error_panic_hook = "=0.1.5"
js-sys = "0.3.19"
lazy_static = "1.3.0"
nalgebra = "0.18.0"
opencascade = "0.2.0"
wasm-bindgen = "0.2.44"
[dependencies.web-sys]
version = "0.3.4"
features = [
'Document',
'Element',
'EventTarget',
'HtmlCanvasElement',
'MouseEvent',
'WebGlBuffer',
'WebGlProgram',
'WebGlRenderingContext',
'WebGlShader',
'WebGlUniformLocation',
'Window',
]
货物更新成功。
npm run build [即“build”:“webpack”] 会出现以下错误
[INFO]: 🌀 Compiling to Wasm...
warning: unused manifest key: env
Blocking waiting for file lock on package cache
Compiling cxx v1.0.122
Compiling wasm-demo v0.1.0 (/home/xxx/learning/wasm)
The following warnings were emitted during compilation:
warning: [email protected]: In file included from /home/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.122/src/cxx.cc:1:
warning: [email protected]: /home/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.122/src/../include/cxx.h:2:10: fatal error: 'algorithm' file not found
warning: [email protected]: #include <algorithm>
warning: [email protected]: ^~~~~~~~~~~
warning: [email protected]: 1 error generated.
error: failed to run custom build command for `cxx v1.0.122`
Caused by:
process didn't exit successfully: `/home/xxx/learning/wasm/target/debug/build/cxx-9e222f77eeee6661/build-script-build` (exit status: 1)
--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-unknown-linux-gnu")
cargo:rerun-if-env-changed=CXX_wasm32-unknown-unknown
CXX_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CXX_wasm32_unknown_unknown
CXX_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CXX
TARGET_CXX = None
cargo:rerun-if-env-changed=CXX
CXX = None
cargo:rerun-if-env-changed=CC_ENABLE_DEBUG_OUTPUT
cargo:rerun-if-env-changed=CRATE_CC_NO_DEFAULTS
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
cargo:rerun-if-env-changed=CXXFLAGS_wasm32-unknown-unknown
CXXFLAGS_wasm32-unknown-unknown = None
cargo:rerun-if-env-changed=CXXFLAGS_wasm32_unknown_unknown
CXXFLAGS_wasm32_unknown_unknown = None
cargo:rerun-if-env-changed=TARGET_CXXFLAGS
TARGET_CXXFLAGS = None
cargo:rerun-if-env-changed=CXXFLAGS
CXXFLAGS = None
cargo:warning=In file included from /home/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.122/src/cxx.cc:1:
cargo:warning=/home/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.122/src/../include/cxx.h:2:10: fatal error: 'algorithm' file not found
cargo:warning=#include <algorithm>
cargo:warning= ^~~~~~~~~~~
cargo:warning=1 error generated.
--- stderr
error occurred: Command "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-std=c++11" "-Wall" "-Wextra" "-o" "/home/xxx/learning/wasm/target/wasm32-unknown-unknown/debug/build/cxx-6e717868d7b84106/out/a01d5fedadde4775-cxx.o" "-c" "/home/xxx/.cargo/registry/src/index.crates.io-6f17d22bba15001f/cxx-1.0.122/src/cxx.cc" with args clang did not execute successfully (status code exit status: 1).
没有 opencascade = "0.2.0" 依赖 npm run 成功。
我的理解是,cargo build 在编译时并不寻找路径。 我添加了 build.rs 脚本但没有成功。 我的示例 build.rs 文件如下
// use std::process::Command;
// fn main() {
// // Set the include paths for the required C++ headers
// let include_paths = ["/usr/include/c++/11" ];
// // Compile the Rust libraries with the required C++ headers
// let mut cmd = Command::new("cargo");
// cmd.arg("build")
// .args(&include_paths.iter().flat_map(|path| vec!["--extra-include-dir", path]).collect::<Vec<&str>>());
// let output = cmd.output().expect("failed to execute cargo build");
// if !output.status.success() {
// panic!("failed to compile Rust libraries:\n{}", String::from_utf8_lossy(&output.stderr));
// }
// }
fn main() {
println!("cargo:rustc-link-search=native=/usr/include/c++/11");
println!("cargo::rustc-env=LIBC_INCLUDE_DIR=/usr/include/c++/11");
println!("cargo::rustc-env=LIBCXX_INCLUDE_DIR=/usr/include/c++/11");
println!("cargo::rustc-env=GLIBC_INCLUDE_DIR=/usr/include/c++/11");
}
// use cc::Build;
// fn main() {
// cc::Build::new()
// .include("/usr/include/c++/11")
// .flag("-g");
// }
// use std::process::Command;
// fn main() {
// // Set the include path for the standard C++ headers
// let output = Command::new("g++")
// .arg("-c")
// .arg("-I/usr/include/c++/11")
// .output()
// .expect("failed to execute process");
// if !output.status.success() {
// panic!("failed to compile C++ code:\n{}", String::from_utf8_lossy(&output.stderr));
// }
// // Link the compiled object file
// println!("cargo:rustc-link-lib=static=cpp_code");
// }
目前在 WebAssembly 模块中混合 Rust 和 C++(或 C)代码还不可行。即使您的 C++ 构建成功,您在链接或执行期间也会失败。
您必须使用仅包含 Rust 代码的库,或者将 C++ 编译为单独的 WebAssembly 模块,并让 JavaScript 提供它与 Rust 模块之间的通信。