Skip to content

Binding c++ with cxx, an error occurs on windows-gnu #137301

Open
@yk0n9

Description

@yk0n9

I upgraded to OCCT 7.9.0 and used cxx-rs to bind the C++ compiled dynamic library. There was no problem with the MSVC version, but the following error occurred with the GNU

Code

// build.rs
fn main() -> std::io::Result<()> {
    let out_env = std::env::var("OUT_DIR").unwrap();
    let out_dir = Path::new(&out_env)
        .parent()
        .unwrap()
        .parent()
        .unwrap()
        .parent()
        .unwrap();

    let (bin, include, lib) = get_occ_dir();

    println!("cargo:rustc-link-search=native={}", lib.display());

    for entry in std::fs::read_dir(lib)? {
        let file = entry?.path();
        if file.is_file() {
            let stem = file.file_stem().unwrap().to_str().unwrap();
            let module = if target_triple::HOST.contains("msvc") {
                stem
            } else {
                stem.strip_prefix("lib").unwrap()
            };
            println!("cargo:rustc-link-lib=static={}", module);
        }
    }

    let mut build = cxx_build::bridge("src/lib.rs");

    if target_triple::TARGET.contains("windows-gnu") {
        build.define("OCC_CONVERT_SIGNALS", "TRUE");
        let mut dll_paths = vec![];
        for dll in [
            "libgcc_s_seh-1.dll",
            "libstdc++-6.dll",
            "libwinpthread-1.dll",
        ] {
            dll_paths.extend(find_mingw_dll(dll));
        }
        for dll in dll_paths {
            copy_file(dll, &out_dir);
        }
    }

    for entry in std::fs::read_dir(bin)? {
        copy_file(entry?.path(), &out_dir);
    }

    if target_triple::HOST.contains("windows") {
        build.include("..");
        println!("cargo:rustc-link-lib=dylib=user32");
    }

    build
        .flag_if_supported("-std=c++17")
        .define("_USE_MATH_DEFINES", "TRUE")
        .include(include)
        .include("include")
        .compile("wrapper");

    println!("cargo:rustc-link-lib=static=wrapper");

    println!("cargo:rerun-if-changed=src/lib.rs");
    println!("cargo:rerun-if-changed=include/wrapper.hxx");

    Ok(())
}

fn get_occ_dir() -> (PathBuf, PathBuf, PathBuf) {
    #[cfg(feature = "occ-build")]
    let occ = occ_build::build();
    #[cfg(not(feature = "occ-build"))]
    let occ = {
        let current_dir = std::env::current_dir().unwrap();
        current_dir.join("OCCT")
    };

    let bin = occ.join("bin");
    let include = occ.join("include");
    let lib = occ.join("lib");

    #[cfg(debug_assertions)]
    #[cfg(feature = "occ-build")]
    if target_triple::HOST.contains("msvc") {
        let bin = occ.join("bind");
        let lib = occ.join("libd");
        return (bin, include, lib);
    }
    (bin, include, lib)
}

Meta

rustc --version --verbose:

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: x86_64-pc-windows-gnu
release: 1.84.1
LLVM version: 19.1.5

Error output

thread 'rustc' panicked at /rust/deps\ar_archive_writer-0.4.2\src\archive_writer.rs:792:30:
called `Result::unwrap()` on an `Err` value: TryFromIntError(())

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.84.1 (e71f9a9a9 2025-01-27) running on x86_64-pc-windows-gnu

note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C strip=debuginfo

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
warning: occ-sys@0.1.0: In file included from D:\code\occ-import\target\release\build\occ-sys-dd7c4f734379bb9d\out\cxxbridge\sources\occ-sys\src\lib.rs.cc:1:
warning: occ-sys@0.1.0: ../occ-sys/include/wrapper.hxx: In function 'const gp_Vec& xyz2vec(const gp_XYZ&)':
warning: occ-sys@0.1.0: ../occ-sys/include/wrapper.hxx:216:58: warning: returning reference to temporary [-Wreturn-local-addr]    
warning: occ-sys@0.1.0:   216 | inline const gp_Vec &xyz2vec(const gp_XYZ &xyz) { return xyz; }
warning: occ-sys@0.1.0:       |                                                          ^~~
error: could not compile `occ-sys` (lib)

Caused by:
  process didn't exit successfully: `D:\Scoop\persist\rustup\.rustup\toolchains\stable-x86_64-pc-windows-gnu\bin\rustc.exe --crate-name occ_sys --edition=2021 occ-sys\src\lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=129 --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --cfg "feature=\"occ-build\"" --check-cfg cfg(docsrs) --check-cfg "cfg(feature, values(\"occ-build\"))" -C metadata=7d22994cf9cff0c3 -C extra-filename=-7d22994cf9cff0c3 --out-dir D:\code\occ-import\target\release\deps -C strip=debuginfo -L dependency=D:\code\occ-import\target\release\deps --extern cxx=D:\code\occ-import\target\release\deps\libcxx-15a77d07e7dd20bf.rmeta -L native=D:\code\occ-import\target\release\build\occ-sys-dd7c4f734379bb9d\out\lib -L native=D:\code\occ-import\target\release\build\occ-sys-dd7c4f734379bb9d\out -l static=TKBin.dll -l static=TKBinL.dll -l static=TKBinTObj.dll -l static=TKBinXCAF.dll -l static=TKBO.dll -l static=TKBool.dll -l static=TKBRep.dll -l static=TKCAF.dll -l static=TKCDF.dll -l static=TKDE.dll -l static=TKDECascade.dll -l static=TKDEGLTF.dll -l static=TKDEIGES.dll -l static=TKDEOBJ.dll -l static=TKDEPLY.dll -l static=TKDESTEP.dll -l static=TKDESTL.dll -l static=TKDEVRML.dll -l static=TKernel.dll -l static=TKFeat.dll -l static=TKFillet.dll -l static=TKG2d.dll -l static=TKG3d.dll -l static=TKGeomAlgo.dll -l static=TKGeomBase.dll -l static=TKHLR.dll -l static=TKLCAF.dll -l static=TKMath.dll -l static=TKMesh.dll -l static=TKOffset.dll -l static=TKPrim.dll -l static=TKRWMesh.dll -l static=TKService.dll -l static=TKShHealing.dll -l static=TKStd.dll -l static=TKStdL.dll -l static=TKTObj.dll -l static=TKTopAlgo.dll -l static=TKV3d.dll -l static=TKVCAF.dll -l static=TKXCAF.dll -l static=TKXMesh.dll -l static=TKXml.dll -l static=TKXmlL.dll -l static=TKXmlTObj.dll -l static=TKXmlXCAF.dll -l static=TKXSBase.dll -l static=wrapper -l static=wrapper -L native=D:\code\occ-import\target\release\build\cxx-ecb1bdbf044545f3\out -L native=D:\code\occ-import\target\release\build\link-cplusplus-7251c9931d89a661\out` (exit code: 101)
Backtrace

   0:     0x7ff9eaaed1b3 - std::backtrace_rs::backtrace::dbghelp64::trace::h7d12d9b19ee258d1
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\..\..\backtrace\src\backtrace/dbghelp64.rs:91:5
   1:     0x7ff9eaaed1b3 - std::backtrace_rs::backtrace::trace_unsynchronized::h0f4ebf753afa50a5
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\..\..\backtrace\src\backtrace/mod.rs:66:5
   2:     0x7ff9eaaed1b3 - std::sys::backtrace::_print_fmt::hb31052709297c0fa
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\sys/backtrace.rs:66:9
   3:     0x7ff9eaaed1b3 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h4cc3f7934b123c8e
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\sys/backtrace.rs:39:26
   4:     0x7ff9eab3df9a - core::fmt::rt::Argument::fmt::hac6f3878855abe6c
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/core\src\fmt/rt.rs:177:76
   5:     0x7ff9eab3df9a - core::fmt::write::ha0bd3b4e85d06134
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/core\src\fmt/mod.rs:1189:21
   6:     0x7ff9eaae31a2 - std::io::Write::write_fmt::h2f2e206c19722672
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\io/mod.rs:1884:15
   7:     0x7ff9eaaed025 - std::sys::backtrace::BacktraceLock::print::he81bbb3c27114a08
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\sys/backtrace.rs:42:9
   8:     0x7ff9eaaef8d7 - std::panicking::default_hook::{{closure}}::h640063efa4a247ec
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src/panicking.rs:268:22
   9:     0x7ff9eaaef712 - std::panicking::default_hook::h30d648e7935cc15e
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src/panicking.rs:295:9
  10:     0x7ff9b12065de - <alloc[8411ff2cd313d67d]::boxed::Box<rustc_driver_impl[cdd40f30474d6fc7]::install_ice_hook::{closure#0}> as core[767214e8a6678fdb]::ops::function::Fn<(&dyn for<'a, 'b> core[767214e8a6678fdb]::ops::function::Fn<(&'a std[c5a75c30f10ea6e9]::panic::PanicHookInfo<'b>,), Output = ()> + core[767214e8a6678fdb]::marker::Sync + core[767214e8a6678fdb]::marker::Send, &std[c5a75c30f10ea6e9]::panic::PanicHookInfo)>>::call
  11:     0x7ff9eaaf00ca - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h8e0885c46e6ac64d
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/alloc\src/boxed.rs:1986:9
  12:     0x7ff9eaaf00ca - std::panicking::rust_panic_with_hook::h7dd330a4ddd4aa09
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src/panicking.rs:809:13
  13:     0x7ff9eaaeff22 - std::panicking::begin_panic_handler::{{closure}}::h2da770a92761b8ca
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src/panicking.rs:674:13
  14:     0x7ff9eaaeda1f - std::sys::backtrace::__rust_end_short_backtrace::h4247fc0670896cf6
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\sys/backtrace.rs:170:18
  15:     0x7ff9eaaefb9e - rust_begin_unwind
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src/panicking.rs:665:5
  16:     0x7ff9eab3a2f1 - core::panicking::panic_fmt::hd1329b78e2ae8db5
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/core\src/panicking.rs:76:14
  17:     0x7ff9eab3a8c0 - core::result::unwrap_failed::hcb3500212c99c5c4
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/core\src/result.rs:1699:5
  18:     0x7ff9b36ac762 - ar_archive_writer[8c4d3102f93bd341]::archive_writer::write_archive_to_stream::<std[c5a75c30f10ea6e9]::fs::File>
  19:     0x7ff9b36346f0 - <rustc_codegen_ssa[d0f34bae28f8725b]::back::archive::ArArchiveBuilder as rustc_codegen_ssa[d0f34bae28f8725b]::back::archive::ArchiveBuilder>::build
  20:     0x7ff9b355232d - rustc_codegen_ssa[d0f34bae28f8725b]::back::link::link_binary
  21:     0x7ff9b16edab3 - <rustc_codegen_llvm[651f8914520d52c9]::LlvmCodegenBackend as rustc_codegen_ssa[d0f34bae28f8725b]::traits::backend::CodegenBackend>::link
  22:     0x7ff9b14459fb - <rustc_interface[2651a7fbd6b9d49e]::queries::Linker>::link
  23:     0x7ff9b11d6dca - rustc_span[2280da7994b0f928]::create_session_globals_then::<core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>, rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_with_globals<rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_pool_with_globals<rustc_interface[2651a7fbd6b9d49e]::interface::run_compiler<core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>, rustc_driver_impl[cdd40f30474d6fc7]::run_compiler::{closure#0}>::{closure#1}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}::{closure#0}::{closure#0}>
  24:     0x7ff9b118496b - std[c5a75c30f10ea6e9]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_with_globals<rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_pool_with_globals<rustc_interface[2651a7fbd6b9d49e]::interface::run_compiler<core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>, rustc_driver_impl[cdd40f30474d6fc7]::run_compiler::{closure#0}>::{closure#1}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>> 
  25:     0x7ff9b121d763 - <<std[c5a75c30f10ea6e9]::thread::Builder>::spawn_unchecked_<rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_with_globals<rustc_interface[2651a7fbd6b9d49e]::util::run_in_thread_pool_with_globals<rustc_interface[2651a7fbd6b9d49e]::interface::run_compiler<core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>, rustc_driver_impl[cdd40f30474d6fc7]::run_compiler::{closure#0}>::{closure#1}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[767214e8a6678fdb]::result::Result<(), rustc_span[2280da7994b0f928]::ErrorGuaranteed>>::{closure#1} as core[767214e8a6678fdb]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  26:     0x7ff9eaaffccd - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hbeb84686bcd00b54
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/alloc\src/boxed.rs:1972:9
  27:     0x7ff9eaaffccd - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h2e8e227ead64960c
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/alloc\src/boxed.rs:1972:9
  28:     0x7ff9eaaffccd - std::sys::pal::windows::thread::Thread::new::thread_start::h5da5085e752f085d
                               at /rustc/e71f9a9a98b0faf423844bf0ba7438f29dc27d58\library/std\src\sys\pal\windows/thread.rs:55:22 
  29:     0x7ffaa38e257d - <unknown>
  30:     0x7ffaa52aaa68 - <unknown>

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️O-windows-gnuToolchain: GNU, Operating system: WindowsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions