Компиляция ржавчины с веббэком для вебсборки

Ошибка возникает, когда я использую зависимость 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 запускается успешно.

Насколько я понимаю, сборка груза не ищет путь во время компиляции. Я добавил скрипт 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");
// }
Почему Python в конце концов умрет
Почему Python в конце концов умрет
Последние 20 лет были действительно хорошими для Python. Он прошел путь от "просто языка сценариев" до основного языка, используемого для написания...
1
0
71
1
Перейти к ответу Данный вопрос помечен как решенный

Ответы 1

Ответ принят как подходящий

В настоящее время невозможно смешивать код Rust и C++ (или C) в модуле WebAssembly. Даже если ваша сборка C++ прошла успешно, у вас возникнут сбои во время компоновки или выполнения.

Вы должны использовать библиотеки, содержащие только код Rust, или скомпилировать C++ в отдельный модуль WebAssembly и использовать JavaScript для обеспечения связи между ним и вашим модулем Rust.

Другие вопросы по теме